如何根据Selenium提供的url单击弹出窗口的Close图标

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何根据Selenium提供的url单击弹出窗口的Close图标相关的知识,希望对你有一定的参考价值。

我正试图摆脱首次访问此页面时出现的弹出窗口:https://www.realestate.com.kh/buy/?listing_type=sale&categories=Condo&order_by=relevance&is_certified=1&private_seller=1#page-10

根据html代码,我希望Selenium“点击”的“x”元素的id是“leadpages-close-button”

enter image description here

所以我试图用这段代码点击它:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Safari()
driver.get("https://www.realestate.com.kh/buy/?listing_type=sale&categories=Condo&order_by=relevance&is_certified=1&private_seller=1#page-10")
driver.find_element_by_id("leadpages-close-button").close()

但它会导致错误:

  File "Untitled.py", line 6, in <module>
    driver.find_element_by_id("leadpages-close-button")
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 359, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 966, in find_element
    'value': value})['value']
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 320, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: 

我究竟做错了什么?

答案

要关闭弹出窗口,首次访问url https://www.realestate.com.kh/buy/?listing_type=sale&categories=Condo&order_by=relevance&is_certified=1&private_seller=1#page-10时弹出窗口,因为弹出窗口位于<iframe>内:

  • 首先,您必须引导WebDriverWait以使所需的帧可用并切换到它
  • 接下来,您必须再次引导WebDriverWait以使所需元素可单击
  • 最后在所需元素上调用click()
  • 代码块: from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.chrome.options import Options options = Options() options.add_argument("start-maximized") options.add_argument("disable-infobars") options.add_argument("--disable-extensions") driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:UtilityBrowserDriverschromedriver.exe') driver.get("https://www.realestate.com.kh/buy/?listing_type=sale&categories=Condo&order_by=relevance&is_certified=1&private_seller=1#page-10") WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[contains(@src,'https://realestatekh.lpages.co/leadbox')]"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//form[contains(@action,'https://realestatekh.activehosted.com/proc.php')]/a[@id='leadpages-close-button']"))).click()

浏览器快照:

realestate_com

另一答案

该元素包含在iframe中,因此您必须先切换到iframe。尝试:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Safari()
driver.get("https://www.realestate.com.kh/buy/?listing_type=sale&categories=Condo&order_by=relevance&is_certified=1&private_seller=1#page-1")
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it(driver.find_element_by_tag_name("iframe")))
#driver.switch_to.frame(driver.find_element_by_tag_name("iframe")) # OR driver.switch_to.frame(0) # switch by iframe index
WebDriverWait(driver,20).until(EC.element_to_be_clickable(driver.find_element_by_id("leadpages-close-button")))
driver.find_element_by_id("leadpages-close-button").click()
driver.switch_to.default_content()

另见:https://www.guru99.com/handling-iframes-selenium.html

另一答案

尝试以下代码切换到动态iframe,然后单击关闭按钮

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait

driver.get("https://www.realestate.com.kh/buy/?listing_type=sale&categories=Condo&order_by=relevance&is_certified=1&private_seller=1#page-10")
wait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(driver.find_element_by_tag_name("iframe")))
driver.find_element_by_id("leadpages-close-button").click()
另一答案

试试:

driver.get("https://www.realestate.com.kh/buy/?listing_type=sale&categories=Condo&order_by=relevance&is_certified=1&private_seller=1#page-10")
time.sleep(1)
driver.find_element_by_id("leadpages-close-button").close()

以上是关于如何根据Selenium提供的url单击弹出窗口的Close图标的主要内容,如果未能解决你的问题,请参考以下文章

我需要创建一个监视selenium执行的进程,如果在执行期间到达,将单击弹出窗口

如何在 Selenium 中处理 IE 设置弹出窗口

如何在运行 Selenium 脚本时禁用 chrome 浏览器中的身份验证弹出窗口

如何关闭弹出窗口并将 url 重定向到父页面?

Selenium - 如何等到页面完全加载[重复]

无法使用任何浏览器的selenium web-driver处理登录弹出窗口