简体   繁体   English

如何使用Selenium WebDriver单击或选择Python中输入下拉框的第一个元素(值)

[英]how to click or select on very first element(value) of input dropdown box in python using selenium webdriver

I am a beginner in python web scraping and trying to automate the site. 我是python网络抓取并尝试使网站自动化的初学者。 In the website which I want to scrape there is a searching filter, where I am applying filters on input box dropdown tag. 在我要抓取的网站上,有一个搜索过滤器,我在输入框下拉标签上应用过滤器。
in the drop-down box, I write the country name and all country come accordingly but I am unable to select or click the very first country that comes as a value in the dropdown box. 在下拉框中,我输入了国家名称,所有国家都相应地出现了,但是我无法选择或单击下拉框中作为值的第一个国家。 I applied some code also but my script fails and terminated. 我也应用了一些代码,但是我的脚本失败并终止。
Below is the image for better understanding. 下面是用于更好理解的图像。 enter image description here 在此处输入图片说明

I also applied some code, please check below 我还应用了一些代码,请在下面检查

p_location='united states'

browser.find_by_xpath("//label[contains(.,'Geography')]").first.click()
        actions_wait_time = randint(15, 30)
        time.sleep(actions_wait_time)
        loc_input = browser.find_by_xpath("//input[@placeholder='Add locations']")
        loc_input.type(p_location)
        actions_wait_time = randint(15, 30)
        time.sleep(actions_wait_time)
        loc_input.type(Keys.TAB)
        time.sleep(3)
        loc_input.type(Keys.RETURN)

I want to select the first value from the dropdown list. 我想从下拉列表中选择第一个值。 Please help 请帮忙

It's not possible to come up with the exact code without seeing the DOM of the page, however one thing is obvious: you should not be using time.sleep function as it is a some form of a performance anti-pattern. 在不查看页面DOM的情况下不可能拿出确切的代码,但是很明显的一点是:您不应该使用time.sleep函数,因为它是某种形式的性能反模式。

You should go for Explicit Wait instead like: 您应该改为“ 显式等待”,例如:

loc_input = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, "//input[@placeholder='Add locations']")))
first_element = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, "//xpath/for/the/first/element/in/the/dropdown")))
first_element.click()

More information: How to use Selenium to test web applications using AJAX technology 详细信息: 如何使用Selenium通过AJAX技术测试Web应用程序

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

相关问题 Python Selenium Webdriver选择下拉值 - Python Selenium Webdriver Select Dropdown Value 如何随机 select 并使用 selenium webdriver(Python)单击元素? - How to randomly select and click on element with selenium webdriver (Python)? 如何使用 Selenium 和 Python 单击值为“下载目录”的输入元素 - How to click on the input element with value as Download Catalogues using Selenium and Python 使用 Python 无法单击 Selenium Webdriver 中的框 - Unable to click on a box in Selenium Webdriver using Python 如何使用 Selenium 和 Python 单击输入元素 - How to click on an input element using Selenium and Python Selenium Webdriver - Python - leboncoin (classified) - pb 选择下拉列表和框 - Selenium Webdriver - Python - leboncoin (classified) - pb to select a dropdown list & box python3如何使用硒单击下拉框 - python3 How do I click dropdown box using selenium 使用Selenium python选择一个下拉框 - select a dropdown box using Selenium python 如果使用 Python Selenium 无法单击第一个元素,如何单击第二个元素 - How to click the second element if the first element is not clickable using Python Selenium 如何在 Python 中使用 Selenium 单击下拉列表的 li 元素^ - How to click on li element of a dropdown list using Selenium in Python^
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM