[英]How to click on li element of a dropdown list using Selenium in Python^
I'm trying to select the li element "US" in this dropdown list of the following website: https://proxyscrape.com/free-proxy-list我正在尝试在以下网站的下拉列表中 select li 元素“US”: https://proxyscrape.com/free-proxy-list
Here is the python code I have but does not work:这是我有但不起作用的 python 代码:
driver.find_element_by_xpath('/html/body/main/div/div[1]/div[3]/div/div[1]/div[2]').click()
time.sleep(4)
driver.find_element_by_css_selector("#list httpcountry countryselect [value='US']")
And here is the HTML I'm working with:这是我正在使用的 HTML:
<div class="nice-select selectortypes open" tabindex="0">
<span>
Country: <span class="current">all</span>
</span>
<ul class="list httpcountry countryselect">
<li data-value="all" class="option">all</li>
<li data-value="US" class="option">US</li>
<li data-value="ES" class="option">ES</li>
<li data-value="RU" class="option">RU</li>
<li data-value="PL" class="option">PL</li>
<li data-value="BD" class="option">BD</li>
<li data-value="IR" class="option">IR</li>
<li data-value="FR" class="option">FR</li>
<li data-value="CN" class="option">CN</li>
<li data-value="CA" class="option">CA</li>
<li data-value="PK" class="option">PK</li>
<li data-value="IN" class="option">IN</li>
<li data-value="ID" class="option">ID</li>
<li data-value="BR" class="option">BR</li>
<li data-value="DE" class="option">DE</li>
<li data-value="GB" class="option">GB</li>
<li data-value="TH" class="option">TH</li>
<li data-value="SG" class="option">SG</li>
<li data-value="EG" class="option">EG</li>
<li data-value="UA" class="option">UA</li>
</ul>
</div>
Any clue on how to select this element?关于如何 select 这个元素的任何线索?
If you look what happens when you select US option, then you can see, that it will change parameters in request here:如果您查看 select US 选项时会发生什么,那么您可以看到,它将在此处更改请求中的参数:
From:从:
<a href="https://api.proxyscrape.com/v2/?request=getproxies&protocol=http&timeout=10000&country=all&ssl=all&anonymity=all&simplified=true" id="downloadhttp" onclick="dataLayer.push({'event': 'GAEvent', 'eventAction': 'downloadfpl', 'eventCategory': 'fpl'});" download="">Download</a>
To:至:
<a href="https://api.proxyscrape.com/v2/?request=getproxies&protocol=http&timeout=10000&country=US&ssl=all&anonymity=all&simplified=true" id="downloadhttp" onclick="dataLayer.push({'event': 'GAEvent', 'eventAction': 'downloadfpl', 'eventCategory': 'fpl'});" download="">Download</a>
So, you actually don't want to click US option, but probably send request with appropriate parameters因此,您实际上不想单击 US 选项,但可能会发送带有适当参数的请求
Solution:解决方案:
You need to wait before clicking on country dropdown list, because of top-banner appears and webdriver losts the focus, dropdown closes.在点击国家下拉列表之前需要等待,因为顶部横幅出现并且 webdriver 失去焦点,下拉关闭。
Here is the code which i wrote and script has passed after adding two sleeps in these two places:这是我编写的代码,脚本在这两个地方添加了两个睡眠后通过了:
driver.get('https://proxyscrape.com/free-proxy-list')
country_list = driver.find_element_by_css_selector('.list.socks4country.countryselect').find_element_by_xpath('./..')
sleep(2)
country_list.click()
sleep(1)
country_list.find_element_by_css_selector('[data-value="US"]').click()
us = driver.find_element_by_css_selector('[class="list socks4country countryselect"] [data-value="US"]')
assert us.get_attribute('class') == 'option selected'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.