栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用python selenium chromedriver从源中选择隐藏的选项值

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用python selenium chromedriver从源中选择隐藏的选项值

from selenium import webdriverfrom selenium.webdriver.common.by import Byimport timefrom selenium.webdriver.support.ui import Selectdriver = 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()

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

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).enpre('utf8'))    return carlist

这是称呼它的方式

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

输出将是:

- Kies merk -AGMAJPApriliaBenelliBetaBMWBTCBullitDerbiDucatiEnergicaGileraHarley DavidsonHeskethHondaHusqvarnaHyosungIndianKawasakiKTMKymcolongjiaMashMorganMorsMoto GuzziMV AgustaNimotoOssaPeugeotPiaggioQuadroRazzoRenaultRoyal EnfieldSachsScomadiSuzukiSWMSYMTriumphTurbhoVespaVictoryVolta MotorbikesYamahaYibenZero Motorcycles


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/403756.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号