首页 > 解决方案 > 无法使用 Selenium 选择下拉值

问题描述

我正在尝试使用此代码选择下拉值之一,但没有任何反应:

selectopt = Select(driver.find_element_by_xpath('/html/body/app-root/html/body/div/div/div[2]/div/div/app-bc-reservation/div/div[1]/div[1]/select'))
selectopt.select_by_value('1cfc3cd0ac77bba30ffac794183100dc220758511a6c943264f8273d90eb54c2')

还尝试通过以下方式选择:

select_by_visible_text
select_by_index

没发生什么事 :(

页面 HTML:

<select _ngcontent-plo-c48="" class="select-text smaller-text">
<option _ngcontent-plo-c48="" hidden="" disabled="" selected="" value=""></option>
<option _ngcontent-plo-c48="" value="1cfc3cd0ac77bba30ffac794183100dc220758511a6c943264f8273d90eb54c2">  Įgijimo deklaravimas/SDK gavimas</option>
<option _ngcontent-plo-c48="" value="bb3ad2ed220c6aeff347669b0eb5c55c756c0203ca2be2fffa02866a9f013e4a"> Perleidimo/sunaikinimo deklaravimas</option>
<option _ngcontent-plo-c48="" value="27b2dcd56aa0800fb8638add9a3924eef8100d3812503626d37101bbaa5efeb5"> Įgijimo perleidimo deklaravimas ir registracija Lietuvoje registruotos transporto priemonės (dalyvaujant pirkėjui ir pardavėjui)</option>
<option _ngcontent-plo-c48="" value="8729a97506baabbd88699aa7178c4993a00627f136e4438e074b20c4bcee2133"> Įgijimo perleidimo deklaravimas ir registracija iš užsienio įvežtos transporto priemonės (dalyvaujant pirkėjui ir pardavėjui)</option>
<option _ngcontent-plo-c48="" value="5df3a2ff0ca9261a8e455037be55bed969c21e6bc930b94ee0cd7f5428ebaa01"> Įgijimo perleidimo deklaravimas ir registracija naujų/laikinai registruojamų transporto priemonių (dalyvaujant pirkėjui ir pardavėjui)</option>
<option _ngcontent-plo-c48="" value="659b0032c6921eacb4e7fcf6b92cc48b584d2fdbd3231e2533a597113ce1e6a6"> Registracija Lietuvoje registruotos transporto priemonės</option>
<option _ngcontent-plo-c48="" value="6646155f52c6f27bb185bdd2c78549a2bac41356e37c4cacb7a668cdad80e3b3"> Registracija iš užsienio įvežtos transporto priemonės</option>
<option _ngcontent-plo-c48="" value="18dab62bb3aff0d11455560ee4e4031efdcf4e3c52101eabe3cf60261a058b79"> Registracija naujos (neregistruotos) transporto priemonės</option>
<option _ngcontent-plo-c48="" value="0360f75da4dcb6d941a0b15d3991397aaffe385883b582f0b2dc2678e00fbe79"> Registracija laikinai registruojamos transporto priemonės</option>
<option _ngcontent-plo-c48="" value="1f4be1860e622e43f8f24885662ab85904d59e1614464757cd78aab962b59f13"> Techninių duomenų keitimas</option>
<option _ngcontent-plo-c48="" value="9731f2a0c9dc91db96efaa9cb1bcb689dbcccc2bb17d4e57d4a2a89570c24b44"> Registracijos liudijimo keitimas</option>
<option _ngcontent-plo-c48="" value="7924d0491cc54487ca6efdf74fdf2e770db4f94a3f8d0d677e32be1528bbf370"> Leidimo dalyvauti viešajame eisme panaikinimas</option>
<option _ngcontent-plo-c48="" value="9e0a394249e8b8f04f14e2ace935b5cd41ac364f27590ea9cbe6a6ceafeaef3a"> Numerio ženklai</option>
<option _ngcontent-plo-c48="" value="1258ac903f436fae73ba186781b0d60601c7b9fe629777a1fb63ca70dd214314"> Pažymų išdavimas</option>
<!----></select>

页面前端: img1 img2

更新:

添加完整代码,稍作改动,仍然是同样的问题。

代码将您定向到该页面,执行一些操作,在最后一页,我需要这个机器人从下拉列表中选择项目,但我无法做到。

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select

driver = webdriver.Chrome()
driver.get('https://eregitra.lt/advance-reservation/service') 

vehicRegBut = driver.find_element_by_xpath('/html/body/app-root/html/body/div/div/div[2]/div/div/app-service/div/div/div/button[1]')
vehicRegBut.click()

OneVehicRegBut = driver.find_element_by_xpath('/html/body/app-root/html/body/div/div/div[2]/div/div/app-service/div/div/div[1]/button')
OneVehicRegBut.click()

NatPersonBut = driver.find_element_by_xpath('/html/body/app-root/html/body/div/div/div[2]/div/div/app-details/div/div/div[1]/button[1]')
NatPersonBut.click()

Name = driver.find_element_by_xpath('/html/body/app-root/html/body/div/div/div[2]/div/div/app-details/div/div[1]/div[1]/input')
Name.send_keys('Name')

SurName = driver.find_element_by_xpath('/html/body/app-root/html/body/div/div/div[2]/div/div/app-details/div/div[1]/div[2]/input')
SurName.send_keys('Surname')

Email = driver.find_element_by_xpath('/html/body/app-root/html/body/div/div/div[2]/div/div/app-details/div/div[1]/div[3]/input')
Email.send_keys('random@gmail.com')

Tel = driver.find_element_by_xpath('/html/body/app-root/html/body/div/div/div[2]/div/div/app-details/div/div[1]/div[4]/div/input')
Tel.send_keys('65555560')

CheckBox = driver.find_element_by_xpath('/html/body/app-root/html/body/div/div/div[2]/div/div/app-details/div/div[2]/div[1]/p-checkbox/input')
CheckBox.click()

NextBut = driver.find_element_by_xpath('/html/body/app-root/html/body/div/div/div[2]/div/div/app-details/div/div[3]/div[2]/button')
NextBut.click()

selectopt = Select(driver.find_element_by_xpath("//select[contains(@class, 'select-text smaller-text')]"))
selectopt.select_by_index(2)

标签: pythonselenium

解决方案


它所需要的只是显式等待。

试试这个:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select

driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://eregitra.lt/advance-reservation/service")
wait = WebDriverWait(driver, 10)

vehicRegBut = driver.find_element_by_xpath('/html/body/app-root/html/body/div/div/div[2]/div/div/app-service/div/div/div/button[1]')
vehicRegBut.click()

OneVehicRegBut = driver.find_element_by_xpath('/html/body/app-root/html/body/div/div/div[2]/div/div/app-service/div/div/div[1]/button')
OneVehicRegBut.click()

NatPersonBut = driver.find_element_by_xpath('/html/body/app-root/html/body/div/div/div[2]/div/div/app-details/div/div/div[1]/button[1]')
NatPersonBut.click()

Name = driver.find_element_by_xpath('/html/body/app-root/html/body/div/div/div[2]/div/div/app-details/div/div[1]/div[1]/input')
Name.send_keys('Name')

SurName = driver.find_element_by_xpath('/html/body/app-root/html/body/div/div/div[2]/div/div/app-details/div/div[1]/div[2]/input')
SurName.send_keys('Surname')

Email = driver.find_element_by_xpath('/html/body/app-root/html/body/div/div/div[2]/div/div/app-details/div/div[1]/div[3]/input')
Email.send_keys('random@gmail.com')

Tel = driver.find_element_by_xpath('/html/body/app-root/html/body/div/div/div[2]/div/div/app-details/div/div[1]/div[4]/div/input')
Tel.send_keys('65555560')

CheckBox = driver.find_element_by_xpath('/html/body/app-root/html/body/div/div/div[2]/div/div/app-details/div/div[2]/div[1]/p-checkbox/input')
CheckBox.click()

NextBut = driver.find_element_by_xpath('/html/body/app-root/html/body/div/div/div[2]/div/div/app-details/div/div[3]/div[2]/button')
NextBut.click()

select_ = wait.until(EC.visibility_of_element_located((By.XPATH, "//select[contains(@class, 'select-text smaller-text')]")))
selectopt = Select(select_)
selectopt.select_by_value('1cfc3cd0ac77bba30ffac794183100dc220758511a6c943264f8273d90eb54c2')

推荐阅读