简体   繁体   English

Selenium 4 找不到元素

[英]Selenium 4 can't find the element

I'm creating rambler autoreg mail using selenium python and encountered a problem that selenium can't find the item by xpath on the page, although it is there.我正在使用 selenium python 创建 rambler autoreg 邮件,遇到了一个问题,即 selenium can't find the item ,尽管它在那里。 It's clear from the error that selenium just can't find the item (error text at the very bottom).从错误中可以清楚地看出 selenium 无法找到该项目(最底部的错误文本)。 Been looking for a solution, but they are mostly all related to another problem, please help you, thanks in advance.一直在寻找解决方案,但它们大多与另一个问题有关,请帮助您,在此先感谢。

HTML HTML

...
<footer class = "styles_popupFooter__1pUND" ...>
    <div class="styles_footer__wCsAz rui-Typography-reset rui-Typography-text">
        <a class="styles_link___lR7s styles_link__lCdad" ... >Registration<a>
        ...
    <div>
    ...
<footer>
    ...

Python Python

# Selenium 4
from selenium import webdriver
from selenium.webdriver.firefox.service import Service

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

class GenerateEmail:
    def __init__(self, driver):
        self.driver = driver
        self.wait = WebDriverWait(driver, 30)

    def main(self):
        url = "https://mail.rambler.ru/?utm_source=head&utm_campaign=self_promo&utm_medium=header&utm_content=mail"
        self.driver.get(url)

        value = '//div[@class="styles_footer__wCsAz rui-Typography-reset rui-Typography-text"]//a[text()="Registration"]'
        self.wait.until(EC.element_to_be_clickable((By.XPATH, value)))
        button = self.driver.find_element(by=By.XPATH, value=value)
        button.click()

def start():
    try:
        s = Service(executable_path='driver/geckodriver.exe')
        driver = webdriver.Firefox(service=s)

        g_email = GenerateEmail(driver)
        g_email.main()
    except Exception as e:
        print(e)
    finally:
        driver.close()

if __name__ == '__main__':
    start()

Error错误

Message:
Stacktrace:
WebDriverError@chrome://remote/content/shared/webdriver/Errors.jsm:186:5
NoSuchElementError@chrome://remote/content/shared/webdriver/Errors.jsm:398:5
element.find/</<@chrome://remote/content/marionette/element.js:300:16

The element Registration you are after is inside an iframe you need switch it first in order to access the element.您要Registration的元素位于iframe中,您需要先切换它才能访问该元素。 Use Webdriverwait() and frame_to_be_available_and_switch_to_it()使用Webdriverwait()frame_to_be_available_and_switch_to_it()

  def main(self):
        url = "https://mail.rambler.ru/?utm_source=head&utm_campaign=self_promo&utm_medium=header&utm_content=mail"
        self.driver.get(url)
        
        self.wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//div[@data-id-frame='embed']//iframe")))

        value = '//div[@class="styles_footer__wCsAz rui-Typography-reset rui-Typography-text"]//a[text()="Registration"]'
        self.wait.until(EC.element_to_be_clickable((By.XPATH, value)))
        button = self.driver.find_element(by=By.XPATH, value=value)
        button.click()

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

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