简体   繁体   English

如何在 Python 中使用 Selenium 单击下拉列表的 li 元素^

[英]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&amp;protocol=http&amp;timeout=10000&amp;country=all&amp;ssl=all&amp;anonymity=all&amp;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&amp;protocol=http&amp;timeout=10000&amp;country=US&amp;ssl=all&amp;anonymity=all&amp;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.

相关问题 如何点击<a>元素</a>列表<ul><a>和</a><li><a>元素与硒使用python?</a> - How to click on the list of the <a> elements in an <ul> and <li> elements with selenium using python? 使用 Python Selenium 在下拉列表中单击选项 - Click on Option in Dropdown List using Python Selenium selenium 和 python 单击下拉列表中的元素 - selenium and python click element in a dropdown 使用 Selenium 和 Python 单击下拉列表中的元素 - Click on element in dropdown with Selenium and Python 如何点击<li>内的元素<ul>使用 Python3 和 Selenium 的列表框?</ul></li> - How to click on <li> element within <ul> listbox using Python3 and Selenium? 如何点击列表中的<li>中的元素<ul> python中含有硒的元素? - How to click on the list of the <li> elements in an <ul> elements with selenium in python? 如何在 python 中使用 Selenium 在 ul 中单击 li? - How do I click an li inside a ul using Selenium in python? 如何使用 Selenium 单击后代 li 元素? - How to click on a descendant li element with Selenium? 如何通过Python和Selenium从下拉菜单中单击元素 - How to click on an element from the Dropdown menu through Python and Selenium 如何使用Selenium和Python单击p-dropdown标记内的元素 - How to Click an Element inside a p-dropdown tag using Selenium and Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM