使用python selenium chromedriver从源中选择隐藏的选项值 [英] Select hidden option value from source using python selenium chromedriver

查看:115
本文介绍了使用python selenium chromedriver从源中选择隐藏的选项值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读取Docx文件[这里是

I am reading a Docx file [here is the link] , parsing some text from that & then Using python selenium bindings & chrome-driver I am trying to click a Hidden option value from source (driver.page_source) . I know it isn't available to select. Here is my code so far :

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请求.设法获得确实包含所需选项数据的响应,请参见此处:

so Using requests module i make a POST request to the link & manage to get a response that does have the needed options data , See here :

<select name="ctl00$cp$ucSearch_Manual$ddlMerk" onchange="updateInputForServerNoPB();InvalidateVehicleSearchResult();setTimeout(&#39;__doPostBack(\&#39;ctl00$cp$ucSearch_Manual$ddlMerk\&#39;,\&#39;\&#39;)&#39;, 0)" id="ctl00_cp_ucSearch_Manual_ddlMerk" class="NormalDropdownlist" style="width:174px;">
        <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 ,以便可以使用 driver 属性遍历选项值? /p>

, I am wondering is there anyway's i can add the above string text to driver.page_source , So that i can iterate over the options values using driver properties ?

推荐答案

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"

此代码会有所帮助. 看到更好的方法是显式等待,但是对于临时解决方案,我使用了time.sleep()

this code will help. See better way is explicit wait but for temp solution i have used time.sleep()

更新:如果要从汽车下拉列表中获取选项,则可以使用以下方法:

Update: If you want to get the option from car dropdown this is a method which can be used:

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

    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")
    carlist =[]
    for option in car.find_elements_by_tag_name('option'):
        carlist.append((option.text).encode('utf8'))
    return carlist

这是一种称呼方式

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

这篇关于使用python selenium chromedriver从源中选择隐藏的选项值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆