【问题标题】:can't click on an element due to google ads由于谷歌广告,无法点击元素
【发布时间】:2020-04-24 15:59:47
【问题描述】:

我一直在尝试使用 selenium 进行自动化操作,需要点击链接,但网页上有谷歌广告,我无法点击按钮。我该如何克服这个问题?

这是代码

elem = browser.find_element_by_link_text('Illinois')
elem.click()

这是错误

ElementClickInterceptedException: Message: element click intercepted: Element <a href="/state/IL">...</a> is not clickable at point (497, 858). Other element would receive the click: <iframe frameborder="0" src="https://tpc.googlesyndication.com/safeframe/1-0-37/html/container.html?2ad90d8b823cd41d3186f3484dae4192" id="google_ads_iframe_/1254144/netronline_com-medrectangle-2_0" title="3rd party ad content" name="" scrolling="no" marginwidth="0" marginheight="0" width="970" height="90" data-is-safeframe="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-top-navigation-by-user-activation" data-google-container-id="12" style="border: 0px; vertical-align: bottom;" data-load-complete="true"></iframe>

我也尝试了 webdriver,但出现超时错误

WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[@href='https://Link.com' and contains(.,'Illinois')]"))).click()

【问题讨论】:

  • 请张贴您的 html 或分享 url 如果这个 pulic?

标签: python selenium automation


【解决方案1】:

试试下面的代码:

linkText= WebDriverWait(browser, 20 ).until( 
EC.element_to_be_clickable(
    (By.LINK_TEXT, "Illinois") ) )
browser.execute_script("arguments[0].click();", linkText)

注意:请在您的解决方案中添加以下导入

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

【讨论】:

  • 它说 self 没有定义
  • 什么没有定义?
  • 我删除了 self 并且它工作了,但是在下一页中,我再次收到超时错误
  • 我不确定你在下一页上要处理什么,我认为你可以使用 webdriver wait 来处理同步问题,如果你已经使用了 wait 那么你可以检查元素是否在 iframe 中。
  • 你能接受我的回答吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-16
  • 1970-01-01
  • 1970-01-01
  • 2018-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多