1

the title says it all, I can't find an element with selenium in python.

This is my code:

    import time
    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.common.by import By
    
    s = Service("C:\Program Files (x86)\Chromedriver.exe")
    driver = webdriver.Chrome(service=s)
    
    driver.get("https://soundcloud.com/signin")
    driver.implicitly_wait(10)
    
    cookies = driver.find_element(By.ID, "onetrust-accept-btn-handler")
    if cookies.is_displayed():
        cookies.click()
        ...
    
    time.sleep(5)
    
    signup = driver.find_element(By.NAME, "Your email address or profile URL")
    signup.send_keys("test")
2
  • 2
    Can you post the stacktrace of the error in detail? Commented Sep 10, 2021 at 20:17
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a minimal reproducible example. Commented Sep 11, 2021 at 1:33

2 Answers 2

2

You are facing that exception

Unable to find element 

cause the desired element is in iframe. So first switch to iframe and then interact with the desired web elements. Also name is email not Your email address or profile URL

Code :

time.sleep(5)
driver.switch_to.frame(driver.find_element(By.XPATH, "//iframe[contains(@src,'https://secure.soundcloud.com/web-auth')]"))
time.sleep(5)
signup = driver.find_element(By.NAME, "email")
signup.send_keys("test")

Also if this works, try to remove time.sleep(5) and use WebDriverWait instead

Sign up to request clarification or add additional context in comments.

1 Comment

Furthur I test this with time.sleep(1) not 5, and it worked great.
0

As already explained, the element signup is in an iframe. You can also use indexing to switch to iframe.

driver.implicitly_wait(10)
driver.get("https://soundcloud.com/signin")
driver.find_element_by_id("onetrust-accept-btn-handler").click()
driver.switch_to.frame(0) # Required singup element is in the 1st iframe.
driver.find_element_by_id("sign_in_up_email").send_keys("Sample Text")

1 Comment

Index switching is not recommend in automation, cause the moment they have a new iframe, index won't work. having said that it is last resort.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.