[英]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.