【问题标题】:how to click or select on very first element(value) of input dropdown box in python using selenium webdriver如何使用selenium webdriver在python中单击或选择输入下拉框的第一个元素(值)
【发布时间】:2019-08-29 07:42:39
【问题描述】:

我是 python 网络抓取和尝试自动化网站的初学者。在我要抓取的网站中有一个搜索过滤器,我在输入框下拉标签上应用过滤器。
在下拉框中,我写下了国家名称,所有国家都相应地出现,但我无法选择或单击下拉框中作为值出现的第一个国家。我也应用了一些代码,但我的脚本失败并终止。
下图是为了更好的理解。 enter image description here

我也应用了一些代码,请在下面查看

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)

我想从下拉列表中选择第一个值。请帮忙

【问题讨论】:

  • 你能提供网址吗?或者至少,通过edit 使用提供的sn-p 工具的html。还请包括您遇到的错误的当前堆栈跟踪。

标签: python selenium-webdriver web-scraping splinter


【解决方案1】:

如果没有看到页面的DOM,就不可能得出确切的代码,但是有一点很明显:你不应该使用time.sleep 函数,因为它是某种形式的性能反模式.

你应该选择Explicit Wait,而不是喜欢:

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()

更多信息:How to use Selenium to test web applications using AJAX technology

【讨论】:

  • 不好意思XPATH id是随机变化的
猜你喜欢
  • 1970-01-01
  • 2021-12-30
  • 2012-09-05
  • 1970-01-01
  • 1970-01-01
  • 2021-04-26
  • 2017-05-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多