使用python selenium chromedri从源代码中选择隐藏选项值

2024-10-03 13:27:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在阅读一个Docx文件[这里是link] ,解析其中的一些文本,然后使用python selenium bindings&chrome驱动程序,我试图从源代码中单击一个隐藏的选项值(driver.page_源) . 我知道不能选择。以下是我目前为止的代码:

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请求并管理以获得包含所需选项数据的响应,请参阅此处:

^{pr2}$

,我想知道是否有任何方法可以将上述字符串文本添加到driver.page_源,以便我可以使用驱动程序属性迭代选项值?在


Tags: fromimportbyvaluedriverelementfindcp
1条回答
网友
1楼 · 发布于 2024-10-03 13:27:52
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import Select


driver = webdriver.Chrome()
driver.maximize_window()
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)


currentselection = driver.find_element_by_xpath(".//*[@id='ctl00_cp_ucSearch_Manual_ddlVoertuigType']")
select = Select(currentselection)
select.select_by_visible_text("Motoren")
time.sleep(5)

try:
    x=driver.find_element_by_xpath(".//*[@id='ctl00_cp_ucSearch_Manual_ddlBouwdag']")
    select = Select(x)
    select.select_by_visible_text("1")

    y=driver.find_element_by_xpath(".//*[@id='ctl00_cp_ucSearch_Manual_ddlBouwmaand']")
    select = Select(y)
    select.select_by_visible_text("1")

    z=driver.find_element_by_xpath(".//*[@id='ctl00_cp_ucSearch_Manual_ddlBouwjaar']")
    select = Select(z)
    select.select_by_visible_text("2017")
    time.sleep(5)


    car = driver.find_element_by_css_selector("#ctl00_cp_ucSearch_Manual_ddlMerk")
    select = Select(car)
    select.select_by_visible_text("BTC")
except:
    print "Not able to select"

这个代码会有所帮助。 看到更好的方法是显式等待,但对于临时解决方案我已经使用时间。睡觉()

更新:如果您想从car dropdown获取选项,这是一种可以使用的方法:

^{pr2}$

这就是所谓的方式

listcar= getallcarlist()
for c in listcar:
    print c

输出将是:

- Kies merk -
AGM
AJP
Aprilia
Benelli
Beta
BMW
BTC
Bullit
Derbi
Ducati
Energica
Gilera
Harley Davidson
Hesketh
Honda
Husqvarna
Hyosung
Indian
Kawasaki
KTM
Kymco
Longjia
Mash
Morgan
Mors
Moto Guzzi
MV Agusta
Nimoto
Ossa
Peugeot
Piaggio
Quadro
Razzo
Renault
Royal Enfield
Sachs
Scomadi
Suzuki
SWM
SYM
Triumph
Turbho
Vespa
Victory
Volta Motorbikes
Yamaha
Yiben
Zero Motorcycles

相关问题 更多 >