【问题标题】:Python Selenium - Getting TimeoutException for elements belonging to a particular section of a webpagePython Selenium - 为属于网页特定部分的元素获取 TimeoutException
【发布时间】:2023-01-08 13:38:40
【问题描述】:

我一直在尝试单击属于网站中切换开关的这个特定按钮。

<button _ngcontent-plj-c265="" type="button" class="glyphicon glyph-mini ng-star-inserted expand" aria-expanded="true" aria-label="Title Expand or collapse filter card" style="visibility: visible;" pbi-focus-tracker-idx="6"></button>

这存在于网站的“过滤器”部分,我采用了几种方法和标签来执行此操作,其中一些我已放在这里:

1. sample1 = ui.WebDriverWait(driver, 60).until(EC.element_to_be_clickable(("xpath", '//*[@id="exploreFilterContainer"]/div[2]/div/filter[14]/div/div[1]/div[1]/button[1]'))).click()

2. sample2 = ui.WebDriverWait(driver, 60).until(EC.element_to_be_clickable(("css selector", 'button.expand'))).click()

3.sample3 = driver.find_element("xpath",'//[@id="exploreFilterContainer"]/div[2]/div/filter[14]/div/div[1]/div[1]/button[1]').click()

方法 3 给了我一个 NoSuchElementException。因此,我采用了方法 1 和 2,但都得到了 TimeoutException。该按钮没有 ID,所以我也无法通过这种方式获取它。

但是我注意到按钮中有一个属性,咏叹调扩展=“真”我假设,当取值“false”时,将有助于操作切换按钮。但要注意的是,只有当 Selenium 识别元素本身时,它才能做到这一点,这让我们回到原点。

对于我的困境,我非常感谢任何富有成效的答案。提前致谢。

【问题讨论】:

  • 你能分享那个页面的链接和你所有的 Selenium 代码吗?
  • @Prophet 好吧,该页面有点机密,恐怕我无法共享链接,但是我所有的 selenium 代码都有助于登录页面并导航到过滤器部分然后发生了这个错误,我所有的代码类似于方法3。网页分为三个部分,其中之一是过滤器部分。
  • 我明白。但如果没有它,我们就无法调试并尝试理解那里发生了什么。
  • 不幸的是,只有按钮代码对超时异常没有帮助——因为如果我们将这段代码放在 HTML 上,它会工作得很好,所以 DOM 上的某些东西会导致超时。我建议在此按钮之前检查 DOM。你可能在那里有一些线索。
  • @simpleApp pyshadow 会有帮助吗?

标签: python html selenium web-scraping timeoutexception


【解决方案1】:

我能够通过切换到 <iframe> 元素来解决我的问题:

driver.switch_to.frame(0)   #switching to the iframe here
tb = driver.find_element("xpath", '//*[@id="exploreFilterContainer"]/div[2]/div/filter[15]/div/div[1]/div[1]/button[1]')
driver.execute_script("arguments[0].click();", tb) #using JS to click the button

并且建议在执行完与元素相关的所有操作后,我们切换回正常状态:

driver.switch_to.default_content()

感谢@Prophet @simpleApp 为帮助我所做的努力:)

【讨论】:

    猜你喜欢
    • 2022-11-28
    • 1970-01-01
    • 2015-12-14
    • 2019-02-03
    • 2020-06-21
    • 2017-12-03
    • 2018-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多