Select hidden option value from source using python selenium chromedriver

前端 未结 1 1231
无人及你
无人及你 2021-01-28 00:10

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 valu

相关标签:
1条回答
  • 2021-01-28 00:43
    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"
    

    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
    

    this is way to call it

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

    the output will be:

    - 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
    
    0 讨论(0)
提交回复
热议问题