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



