简体   繁体   English

如何在 python 中使用 selenium 下拉列表的 select 值

[英]How to select value of drop down list with selenium in python

I have just started my first project with python and already spend hours searching for a solution, but couldn't find anything.我刚刚开始使用 python 开始我的第一个项目,并且已经花费数小时寻找解决方案,但找不到任何东西。

I would like to fill out a website, by selecting values from drop-down list.我想通过从下拉列表中选择值来填写网站。 My problem is that the values of these lists are not hard coded like in the solutions I found in this forum.我的问题是这些列表的值不像我在这个论坛中找到的解决方案那样硬编码。

I have inspected the website and made three screenshots (before clicking, when clicking and after selecting an item)我检查了网站并制作了三个屏幕截图(点击前、点击时和选择项目后)

before clicking点击之前

by cklicking通过点击

after selecting选择后

I was able to open the drop-down-list and read all values of the list, but not able to select one of them我能够打开下拉列表并读取列表的所有值,但无法 select 其中之一

css = 'body > main > section > section > div.ut-navigation-container-view--content > div > div.ut-pinned-list-container.ut-content-container > div > div.ut-pinned-list > div.ut-item-search-view > div:nth-child(2)'
iElement = browser.find_element_by_css_selector(css)
SpanVariableValue = iElement.text
#prints selected value or default 
print (SpanVariableValue )

iElement.click()
SpanVariableValue = iElement.text
#prints all elemets of drop-down
print (SpanVariableValue)

#drop-down doesn't work after the following code 
browser.execute_script("arguments[0].innerText = 'SILVER'", iElement)

Can anybody help me with my problem?有人可以帮我解决我的问题吗?

Cheers, Andi干杯,安迪

Edit: Sorry, I made a mistake when adding the screenshots in the original post编辑:对不起,我在原帖中添加截图时出错了

Not sure if you already attempted this when looking through the Selenium documentation, you could give the following a try:不确定您在查看 Selenium 文档时是否已经尝试过此操作,您可以尝试以下操作:

Selenium.click()

An example command would look something like this:示例命令如下所示:

element = driver.find_element :xpath, '//input[@name="q"]'
element.click()

You can just set the element xpath to be the elements of the drop down, then Selenium would be able to click the options you want.您只需将元素 xpath 设置为下拉元素,然后 Selenium 就可以单击您想要的选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM