简体   繁体   English

在 Python 上使用 Selenium 在弹出窗口 Window 上找不到元素

[英]Can't locate element on Pop-Up Window using Selenium on Python

I want to scrape some elements from the Duden webpage with this url: https://www.duden.de/rechtschreibung/aussuchen .我想用这个 url: https://www.duden.de/rechtschreibung/aussuchen从 Duden 网页上抓取一些元素。 When I look up the page manually no pop-up occures but when I use selenium on python this occures: image of pop up当我手动查找页面时,不会出现弹出窗口,但是当我在 python 上使用 selenium 时,会出现这种情况:弹出窗口的图像

I already tried a lot of things like blocking pop ups in general, or trying to click on the accept button.我已经尝试了很多东西,比如阻止弹出窗口,或者尝试点击接受按钮。 All of that is not working.所有这些都不起作用。

I tried to find an element of the frame and print a statement then to see whether it can find the elements but that is also not working.我试图找到框架的一个元素并打印一条语句,然后查看它是否可以找到这些元素,但这也不起作用。

Has anyone an idea why it is like that or what I could try more?有谁知道为什么会这样或者我可以尝试更多吗?

These are a few things I tried:这些是我尝试过的一些事情:

  • For blocking:对于阻塞:

     def getAllWordForms(word): options = Options() profile = webdriver.FirefoxProfile() profile.set_preference("dom.disable_open_during_load", False) driver = webdriver.Firefox(firefox_profile=profile,options=options, executable_path=os.path.join(driver_location, 'geckodriver')) main_url = 'https://www.duden.de/rechtschreibung/' word_url = main_url + '{}'.format(word) driver.get(word_url)
  • to see if it can find an element in the pop up frame:看看它是否可以在弹出框中找到一个元素:

     def getAllWordForms(word): options = Options() driver = webdriver.Firefox(options=options, executable_path=os.path.join(driver_location, 'geckodriver')) main_url = 'https://www.duden.de/rechtschreibung/' word_url = main_url + '{}'.format(word) driver.get(word_url) driver.implicitly_wait(10) driver.switch_to.frame(1) if driver.find_elements_by_class_name('message-button'): print('yes')
  • to click the button:点击按钮:

     def getAllWordForms(word): options = Options() options.headless = False driver = webdriver.Firefox(options=options, executable_path=os.path.join(driver_location, 'geckodriver')) main_url = 'https://www.duden.de/rechtschreibung/' word_url = main_url + '{}'.format(word) driver.get(word_url) driver.implicitly_wait(10) driver.switch_to.frame(1) button = driver.find_element_by_xpath("//button[@aria-label='AKZEPTIEREN']") button.click() driver.switch_to.default_content()

I tried out various combinations, but it never works.我尝试了各种组合,但它从来没有奏效。

The elements of the page are structred like this: structure of page_1 structure of page_2页面的元素结构如下: page_1的结构page_2 的结构

Hope I could explain it correct and that maybe someone could help me.希望我能正确解释它,也许有人可以帮助我。

Every time you launch your webdriver you're using a new temporary profile.每次启动 webdriver 时,您都在使用新的临时配置文件。 That profile has no cookies therefore it's seen by the site as a new user an needs to accept the cookie message.该配置文件没有 cookies 因此站点将其视为需要接受 cookie 消息的新用户。

I had a look at your site and to close the message you need to switch iframe.我查看了您的站点并关闭了您需要切换 iframe 的消息。 You were close with your solution, it might just be it needed a different method of selecting the frame...你对你的解决方案很接近,它可能只是需要一种不同的选择框架的方法......

This code works for me:这段代码对我有用:

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

driver = webdriver.Chrome()
driver.get("https://www.duden.de/rechtschreibung/aussuchen")

iframe = driver.find_element_by_xpath("//iframe[contains(@id,'sp_message_iframe')]")
driver.switch_to.frame(iframe)
cookieAccpet = WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='AKZEPTIEREN']")))
cookieAccpet.click()

driver.switch_to.default_content()

Remember to switch back to the default frame at the end with driver.switch_to.default_content() , then you can continue your script.记得在最后用driver.switch_to.default_content()切换回默认帧,然后你可以继续你的脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM