本文将介绍使用Selenium和Python列出选择选项值的详细情况,特别是关于pythonseleniumselect的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,
本文将介绍使用Selenium和Python列出选择选项值的详细情况,特别是关于python selenium select的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于Selenium和Python查找元素和文本?、从弹出窗口中使用Selenium和python下载并保存多个csv文件、从弹出窗口使用selenium和python下载并保存多个csv文件、使用python selenium chromedriver从源中选择隐藏的选项值的知识。
本文目录一览:- 使用Selenium和Python列出选择选项值(python selenium select)
- Selenium和Python查找元素和文本?
- 从弹出窗口中使用Selenium和python下载并保存多个csv文件
- 从弹出窗口使用selenium和python下载并保存多个csv文件
- 使用python selenium chromedriver从源中选择隐藏的选项值
使用Selenium和Python列出选择选项值(python selenium select)
我有以下HTML代码
<select name="countries" class_id="countries"> <option value="-1">--SELECT COUNTRY--</option> <option value="459">New Zealand</option> <option value="100">USA</option> <option value="300">UK</option></select>
我正在尝试使用Selenium获取选项值的列表(例如459、100等,而不是文本)。
目前,我有以下Python代码
from selenium import webdriverdef country_values(website_url): browser = webdriver.Firefox() browser.get(website_url) html_code=browser.find_elements_by_xpath("//select[@name=''countries'']")[0].get_attribute("innerHTML") return html_code
如您所见,代码返回纯HTML,我正在使用HTMLParser库进行解析。有什么方法可以仅使用Selenium来获取选项值?换句话说,不必解析Selenium的结果吗?
答案1
小编典典检查一下,这是我做的,然后才知道选择模块做了什么
from selenium import webdriverbrowser = webdriver.Firefox()#code to get you to the pageselect_box = browser.find_element_by_name("countries") # if your select_box has a name.. why use xpath?..... # this step could use either xpath or name, but name is sooo much easier.options = [x for x in select_box.find_elements_by_tag_name("option")]# this part is cool, because it searches the elements contained inside of select_box # and then adds them to the list options if they have the tag name "options"for element in options: print element.get_attribute("value") # or append to list or whatever you want here
这样的输出
-1459100300
Selenium和Python查找元素和文本?
当我转到某个网页时,我正在尝试查找某个元素和一段文本:
<span>0</span>
这不起作用:( 它给出了复合类名的错误…)
elem = browser.find_elements_by_class_name("Bold Orange Large")
所以我尝试了这个:( 但是我不确定它是否有效,因为我不太了解在selenium中使用CSS选择器的正确方法…)
elem = browser.find_elements_by_css_selector("span[]")
找到span元素后,我想找到其中的数字 (内容) 。
num = elem.(what to put here??)
CSS选择器,类名和查找元素文本的任何帮助都将非常有用!
谢谢。
哦! 我的另一个问题是,其中有多个确切的span元素,但内部有不同的数字。我该如何处理?
从弹出窗口中使用Selenium和python下载并保存多个csv文件
我想从“
https://clinicaltrials.gov/ct2/results?cond=&term=lomitapide&cntry1=&state1=&SearchAll=Search+all+studies&recrs=
”网站下载csv文件,如下所示:我收到异常“ ElementNotInteractableException”,无法下载页面
from selenium import webdriver fp=webdriver.FirefoxProfile() fp.set_preference("browser.helperApps.neverAsk.saveToDisk","text/csv") browser = webdriver.Firefox(fp) browser.get("https://clinicaltrials.gov/ct2/results?cond=&term=lomitapide&cntry1=&state1=&SearchAll=Search+all+studies&recrs=") browser.find_element_by_id("submit-download-list")
答案1
小编典典这是您问题的答案:
您所说的元素find_element_by_id("submit-download-list")
实际上是下载PDF
文件。因此,为了将来程序员和对此问题/帖子/线程/讨论的读者的利益,您可以考虑将问题标题更改为 Downloadand Save PDF file using selenium and python from popup
这是从弹出窗口使用selenium和python下载和保存PDF文件的代码块:
import osfrom selenium import webdriverfrom selenium.webdriver.firefox.firefox_binary import FirefoxBinaryfrom selenium.webdriver.common.desired_capabilities import DesiredCapabilitiesfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.by import Bybinary = FirefoxBinary(''C:\\Program Files\\Mozilla Firefox\\firefox.exe'')newpath = ''C:\\home\\DebanjanB''if not os.path.exists(newpath): os.makedirs(newpath)profile = webdriver.FirefoxProfile()profile.set_preference("browser.download.dir",newpath)profile.set_preference("browser.download.folderList",2)profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain,text/x-csv,text/csv,application/vnd.ms-excel,application/csv,application/x-csv,text/csv,text/comma-separated-values,text/x-comma-separated-values,text/tab-separated-values,application/pdf")profile.set_preference("browser.download.manager.showWhenStarting",False)profile.set_preference("browser.helperApps.neverAsk.openFile","text/plain,text/x-csv,text/csv,application/vnd.ms-excel,application/csv,application/x-csv,text/csv,text/comma-separated-values,text/x-comma-separated-values,text/tab-separated-values,application/pdf")profile.set_preference("browser.helperApps.alwaysAsk.force", False)profile.set_preference("browser.download.manager.useWindow", False)profile.set_preference("browser.download.manager.focusWhenStarting", False)profile.set_preference("browser.helperApps.neverAsk.openFile", "")profile.set_preference("browser.download.manager.alertOnEXEOpen", False)profile.set_preference("browser.download.manager.showAlertOnComplete", False)profile.set_preference("browser.download.manager.closeWhenDone", True)profile.set_preference("pdfjs.disabled", True)caps = DesiredCapabilities.FIREFOXbrowser = webdriver.Firefox(firefox_profile=profile, capabilities=caps, firefox_binary=binary, executable_path=''C:\\Utility\\BrowserDrivers\\geckodriver.exe'')browser.maximize_window()browser.get("https://clinicaltrials.gov/ct2/results?cond=&term=lomitapide&cntry1=&state1=&SearchAll=Search+all+studies&recrs=")browser.find_element_by_id("save-list-link").click()download_link = WebDriverWait(browser, 10).until( EC.presence_of_element_located((By.XPATH, "//input[@id=''submit-download-list'']")))download_link.click()
让我知道这是否回答了您的问题。
从弹出窗口使用selenium和python下载并保存多个csv文件
我想从“
https://clinicaltrials.gov/ct2/results?cond=&term=lomitapide&cntry1=&state1=&SearchAll=Search+all+studies&recrs=
“网站上下载csv文件,如下所示:我收到异常“ ElementNotInteractableException”,无法下载页面
from selenium import webdriver
fp=webdriver.FirefoxProfile()
fp.set_preference("browser.helperApps.neverAsk.saveToDisk","text/csv")
browser = webdriver.Firefox(fp)
browser.get("https://clinicaltrials.gov/ct2/results?cond=&term=lomitapide&cntry1=&state1=&SearchAll=Search+all+studies&recrs=")
browser.find_element_by_id("submit-download-list")
使用python selenium chromedriver从源中选择隐藏的选项值
我正在阅读Docx文件这里是[链接],从中解析一些文本,然后使用pythonselenium绑定和chrome-
driver我试图从源(driver.page_source)中单击“隐藏”选项值。我知道无法选择。到目前为止,这是我的代码:
import time,re
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from docx import opendocx,getdocumenttext
from requests import Session
def read_return(word_file):
document = opendocx(word_file)
paratextlist = getdocumenttext(document)
newparatextlist = []
for paratext in paratextlist:
newparatextlist.append((paratext.encode("utf-8")).strip('\n').strip('\t').strip('\r'))
newparatextlist = str(newparatextlist).replace("]","").replace("[","")
with open('sample.txt','wb')as writer:
writer.write(newparatextlist)
return newparatextlist
word_file = read_return('Taxatierapport SEK - Auto Centrum Bollenstreek - Peugeot 308 - 5603.docx')
x = lambda x:re.findall(x,word_file,re.DOTALL)[0].strip().replace("'","'").replace('"',''')
Voertuig = x("::OBJECT::','(.+?)'")
Merk = x("::MERK::','(.+?)'")
Model = x("::TYPE::','(.+?)'")
TOELATING = x("::BOUWJAAR 1STE TOELATING::','(.+?)'")
d1 = TOELATING.split("-")[0]
d2 = TOELATING.split("-")[1]
d3 = TOELATING.split("-")[2]
TRANSMISSIE = x("::TRANSMISSIE::','(.+?)'")
BRANDSTOF = x("::BRANDSTOF::','(.+?)'")
print "%r\n%r\n%r\n%r\n%r\n%r\n%r\n%r\n" %(Voertuig,Merk,Model,d1,d2,d3,TRANSMISSIE,BRANDSTOF)
if Voertuig == "Personenauto":
value = 1
elif Voertuig == "Personenbussen":
value = 7
elif Voertuig == "Bedrijfsauto's tot 3.5 ton":
value = 3
elif Voertuig == "Bedrijfsauto's 4x4":
value = 2
elif Voertuig == "Motoren":
value= 5
xr = 0; yr = 0; zr = 1972
while xr < 32:
if int(d1) == xr:
dvalue1 = xr
else:
pass
xr+=1
while yr < 13:
if int(d2) == yr:
dvalue2 = yr
else:
pass
yr+=1
while zr < 2018:
if int(d3) == zr:
dvalue3 = zr
else:
pass
zr+=1
driver = webdriver.Chrome('chromedriver.exe')
driver.get('https://autotelexpro.nl/LoginPage.aspx')
driver.find_element(By.XPATH,value ='//*[@id="ctl00_cp_LogOnView_LogOn_txtVestigingsnummer"]').send_keys('3783')
driver.find_element(By.XPATH,value ='//*[@id="ctl00_cp_LogOnView_LogOn_txtGebruikersnaam"]').send_keys('Frank')
driver.find_element(By.XPATH,value ='//*[@id="ctl00_cp_LogOnView_LogOn_Password"]').send_keys('msnauto2016')
driver.find_element(By.XPATH,value ='//*[@id="ctl00_cp_LogOnView_LogOn_btnLogin"]').click()
time.sleep(10)
#try:
driver.find_element(By.XPATH,value ='//select[@name="ctl00$cp$ucSearch_Manual$ddlVoertuigType"]/option[@value="'+str(value)+'"]').click()
driver.find_element(By.XPATH,value ='//select[@name="ctl00$cp$ucSearch_Manual$ddlBouwdag"]/option[@value="'+str(dvalue1)+'"]').click()
driver.find_element(By.XPATH,value ='//select[@name="ctl00$cp$ucSearch_Manual$ddlBouwmaand"]/option[@value="'+str(dvalue2)+'"]').click()
driver.find_element(By.XPATH,value ='//select[@name="ctl00$cp$ucSearch_Manual$ddlBouwjaar"]/option[@value="'+str(dvalue3)+'"]').click()
driver.find_element(By.XPATH,value ='//select[@name="ctl00$cp$ucSearch_Manual$ddlMerk"]/option[@value="130"]').click()
#except:
driver.quit()
time.sleep(5)
driver.quit()
所以使用请求模块,我向链接发出POST请求并设法获得包含所需选项数据的响应,请参见此处:
<select name="ctl00$cp$ucSearch_Manual$ddlMerk" onchange="updateInputForServerNoPB();InvalidateVehicleSearchResult();setTimeout('__doPostBack(\'ctl00$cp$ucSearch_Manual$ddlMerk\',\'\')',0)" id="ctl00_cp_ucSearch_Manual_ddlMerk">
<option selected="selected" value="-1">- Kies merk -</option>
<option value="95">Alfa Romeo</option>
<option value="154">Aston Martin</option>
<option value="96">Audi</option>
<option value="97">Bentley</option>
<option value="98">BMW</option>
<option value="352">Bugatti</option>
<option value="100">Cadillac</option>
<option value="342">Chevrolet</option>
<option value="101">Chevrolet USA</option>
<option value="102">Chrysler</option>
<option value="103">Citroen</option>
<option value="337">Corvette</option>
<option value="104">Dacia</option>
<option value="105">Daihatsu</option>
<option value="166">Daimler</option>
<option value="162">Dodge</option>
<option value="106">Donkervoort</option>
<option value="107">Ferrari</option>
<option value="108">Fiat</option>
<option value="94">Ford</option>
<option value="111">Honda</option>
<option value="340">Hummer</option>
<option value="112">Hyundai</option>
<option value="365">Infiniti</option>
<option value="113">Jaguar</option>
<option value="114">Jeep</option>
<option value="150">Kia</option>
<option value="115">Lada</option>
<option value="116">Lamborghini</option>
<option value="117">Lancia</option>
<option value="168">Land Rover</option>
<option value="432">Landwind</option>
<option value="118">Lexus</option>
<option value="119">Lotus</option>
<option value="120">Maserati</option>
<option value="330">Maybach</option>
<option value="121">Mazda</option>
<option value="122">Mercedes-Benz</option>
<option value="304">Mini</option>
<option value="124">Mitsubishi</option>
<option value="126">Morgan</option>
<option value="127">Nissan</option>
<option value="128">Opel</option>
<option value="130">Peugeot</option>
<option value="132">Porsche</option>
<option value="134">Renault</option>
<option value="135">Rolls-Royce</option>
<option value="138">Saab</option>
<option value="139">Seat</option>
<option value="140">Skoda</option>
<option value="226">smart</option>
<option value="343">Spyker</option>
<option value="210">SsangYong</option>
<option value="141">Subaru</option>
<option value="142">Suzuki</option>
<option value="417">Think</option>
<option value="144">Toyota</option>
<option value="147">Volkswagen</option>
<option value="145">Volvo</option>
</select>
,我想知道是否可以将上述字符串文本添加到 driver.page_source中 ,以便可以使用 驱动程序 属性遍历选项值?
今天的关于使用Selenium和Python列出选择选项值和python selenium select的分享已经结束,谢谢您的关注,如果想了解更多关于Selenium和Python查找元素和文本?、从弹出窗口中使用Selenium和python下载并保存多个csv文件、从弹出窗口使用selenium和python下载并保存多个csv文件、使用python selenium chromedriver从源中选择隐藏的选项值的相关知识,请在本站进行查询。
本文标签: