Home > Back-end >  Python Selenium cannot locate element
Python Selenium cannot locate element

Time:06-21

I am trying to automate login process to a website. There are no issues with finding the first element:

driver.find_element(By.ID, 'topbar-cta-btn').click()

After clicking the first element, a new page loads. On a new page there is a different button that Selenium cannot locate.

Here is a full code:

from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

chrome_options = Options()
s = Service(r"somepath")
chrome_options.add_experimental_option("detach", True)
chrome_options.add_argument("--incognito")

login = "login"
pwd = "password"

def run():
    driver = webdriver.Chrome(service=s, options=chrome_options)
    driver.get("https://www.zalando-lounge.pl/")
    driver.find_element(By.ID, 'topbar-cta-btn').click()   # 1st button - works fine
    driver.find_element(By.ID, 'sso-login-lounge').click()   # 2nd button - the issue occurs
    driver.find_element(By.ID, 'form-email').send_keys(login)
    driver.find_element(By.ID, 'form-password').send_keys(pwd)
    driver.find_element(By.ID, 'login-form-submit').click()

run()

The interesting thing is, when you inspect button for the first time, it opens with highlighted wrapped body:

<body  style=""><div id="lightningjs-usabilla_live" style="display: none;"><div><iframe frameborder="0" id="lightningjs-frame-usabilla_live"></iframe></div></div>

inspecting second time:

    <button color="dark" type="button" aria-labelledby="sso-login-lounge" ><span id="sso-login-lounge" ><i ><svg color="#FFFFFF" width="24px" height="24px" viewBox="0 0 24 24" role="img" aria-hidden="true" focusable="false" ><path fill-rule="evenodd" clip-rule="evenodd" d="M4 4.75h16A2.25 2.25 0 0 1 22.25 7v10A2.25 2.25 0 0 1 20 19.25H4A2.25 2.25 0 0 1 1.75 17V7A2.25 2.25 0 0 1 4 4.75zM3 7c0-.063.006-.126.017-.186L9.5 12l-6.482 5.186A1.006 1.006 0 0 1 3 17V7zm1 11h16l-6.5-5.2-.485.388a1.625 1.625 0 0 1-2.03 0L10.5 12.8 4 18zm6.902-6.48.864.692c.137.11.331.11.468 0l.865-.692a.706.706 0 0 1 .022-.017L20 6H4l6.878 5.502a.593.593 0 0 1 .024.019zM14.5 12l6.482 5.186c.011-.06.017-.122.017-.186V7c0-.063-.006-.126-.017-.186L14.5 12z" fill="inherit"></path></svg></i><span>Zaloguj się</span></span></button>

Is there something built in the page that prevents locating the second button?

CodePudding user response:

Try the below to click on the button

driver.find_element(By.XPATH, "//button[@aria-labelledby='sso-login-lounge']").click()

OR

driver.find_element(By.XPATH, "//button[contains(@class,'Wrapper-sc-8so8sv eyQKCA')]").click()

OR

element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(@aria-labelledby,'sso-login-lounge')]")))

element.click();

Do not forget to import the below

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

CodePudding user response:

As suggested by @UserAG putting sleep() between each search did the trick.

  • Related