Selenium 4 can't find the element

Viewed 140

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. It's clear from the error that selenium just can't find the item (error text at the very bottom). Been looking for a solution, but they are mostly all related to another problem, please help you, thanks in advance.

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

# 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
1 Answers

The element Registration you are after is inside an iframe you need switch it first in order to access the element. Use Webdriverwait() and 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()
Related