Home > Enterprise >  Why is this Xpath not working with selenium?
Why is this Xpath not working with selenium?

Time:05-23

I'm trying to input an email address to test logging in, but I'm continuing to receive this error: no such element: Unable to locate element:

I have tried using the relative and absolute Xpath and receive the same error message.

Forgive me as I'm sure missing something simple, very new to this!

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
import time

driver = webdriver.Chrome()

url = 'https://soundcloud.com/signin'

driver.get(url)

time.sleep(2)

driver.find_element_by_xpath('//*[@id="sign_in_up_email"]').send_keys('[email protected]')

CodePudding user response:

The reason its throwing error is because the element sign_in_up_email is present inside the iframe Refer image

Check the link here for detail about how to switch to iframe

You will first need to switch to iframe and then enter value in the input

Note:- When you first open the page you might see the accept cookies popup from soundcloud you will have to accept that

Your solution would look like

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
import time

driver = webdriver.Chrome()

url = 'https://soundcloud.com/signin'

driver.get(url)
driver.maximize_window()
time.sleep(2)
# Accept cookie
driver.find_element_by_id('onetrust-accept-btn-handler').click()
# Switch to frame
driver.switch_to.frame(driver.find_element_by_class_name("webAuthContainer__iframe"))
driver.find_element_by_xpath('//*[@id="sign_in_up_email"]').send_keys('[email protected]')
  • Related