【问题标题】:How to find elements on a JavaScript Website with Selenium?如何使用 Selenium 在 JavaScript 网站上查找元素?
【发布时间】:2020-04-07 08:58:44
【问题描述】:

我想为自己自动搜索一些东西,但我在这里遇到了一点问题。 在这个网站上:

https://shop.orgatop.de/

程序找不到搜索栏,我也不知道为什么。

driver = webdriver.Firefox()
driver.get('https://shop.orgatop.de/')
input_search = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="solrSearchTerm"]')))
input_search.click()
input_search.send_keys('asd')
input_search.send_keys(Keys.RETURN)

【问题讨论】:

    标签: javascript python selenium automation webdriverwait


    【解决方案1】:

    该元素存在于嵌套 iframe 中,例如 innerFrame>catalog>content>input。您需要先切换这些框架才能访问输入搜索框。

    诱导WebDriverWait()和frame_to_be_available_and_switch_to_it()

    driver = webdriver.Firefox()
    driver.get('https://shop.orgatop.de/')
    WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"innerFrame")))
    WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"catalog")))
    WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"content")))
    input_search = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="solrSearchTerm"]')))
    input_search.click()
    input_search.send_keys('asd')
    input_search.send_keys(Keys.RETURN)
    

    浏览器快照:

    【讨论】:

    • 它给了我一个错误:文件“C:\Users\Aron\Desktop\apa\kereso2.py”,第 28 行,在 WebDriverWait(driver,1 0).until(EC .frame_to_be_available_and_switch_to_it((By.NAME,"content"))) 文件“C:\Python\lib\site-packages\selenium\webdriver\support\wait.py”,第 80 行,直到引发 TimeoutException(消息,屏幕,堆栈跟踪)
    • @Pilv :现在尝试更新的答案。元素存在于嵌套框架中。让我知道这是怎么回事。它对我有用。
    • 谢谢!有用。如果要求不高,您能解释一下为什么会这样吗?
    • @Pilv :请阅读我的答案描述。我已经提到您正在与之交互的输入搜索框位于嵌套的框架元素内。因此 selenium 无法直接交互这种输入,直到您切换那些首先是帧。这就是我在这段代码中所做的。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多