Selenium Cannot Locate element by id - python

The below is a selenium python code where I am trying to click Sign In by sending the login details via selenium. However, when I am using find_element_by_id method to locate the username and password input area the scripts throws an error
Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="usernameOrEmail"]"}. But, when I am inspect the webpage on the input text type it shows me the same id which I have mentioned in my script.
P.S: When the selenium opens up the browser please maximize the windows else the code will not work
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome(executable_path='C://Arighna/chromedriver.exe')
driver.get("https://www.fool.com/")
print(driver.title)
mybutton = driver.find_element_by_id('login-menu-item')
mybutton.click()
delay = 5
WebDriverWait(driver,delay)
email_area = driver.find_element_by_id('usernameOrEmail')
email.send_keys(Keys.ENTER)
email_area.send_keys('ar')
WebDriverWait(driver,delay)
pwd_area = driver.find_element_by_id('password')
pwd_area.send_keys(Keys.ENTER)
pwd_area.send_keys('1234')
WebDriverWait(driver,delay)
login_btn = driver.find_element_by_id('btn-login')
login_btn.click()
Any help is really appreciated.

I modified the above code as this one and it worked.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options, executable_path='C://Arighna//chromedriver.exe')
driver.get("https://www.fool.com/")
mybutton = driver.find_element_by_id('login-menu-item')
mybutton.click()
WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//input[#id='usernameOrEmail']"))
)
driver.find_element_by_xpath("//input[#id='usernameOrEmail']").send_keys("Selenium")
WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//input[#id='password']"))
)
driver.find_element_by_xpath("//input[#id='password']").send_keys("Selenium")
singin_bttn = driver.find_element_by_id('btn-login')
singin_bttn.click()

To send a character sequence to the Email / Username field you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get('https://www.fool.com/')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#login-menu-item"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#usernameOrEmail"))).send_keys("SouradipRoy")
Using XPATH:
driver.get('https://www.fool.com/')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='login-menu-item']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='usernameOrEmail']"))).send_keys("SouradipRoy")
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
References
You can find a couple of relevant discussions on NoSuchElementException in:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element

Related

Python Selenium: Unable to locate element with id

I'm trying to select a button but I keep getting the error of:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="loginbutton"]"}
(Session info: chrome=103.0.5060.134)
the id of the button is correct, not sure what i'm doing wrong.
I'm running this code
chrome_options = webdriver.ChromeOptions()
chrome_options.headless = False
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches",["enable-automation"])
driver = webdriver.Chrome(options=chrome_options)
def initializedBrowser():
driver.get("https://www.facebook.com/")
time.sleep(3)
initializedBrowser()
emailelement = driver.find_element(By.ID, 'email')
emailelement.send_keys("testing")
logginButton = driver.find_element(By.ID, 'loginbutton')
logginButton.click()
But i see it exist here in this image
Try:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.ID, "loginbutton"))).click()
You will also need the following imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
The desired element is a dynamic element, so to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#loginbutton[name='login']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#id='loginbutton' and #name='login']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

How to locate the first channel points button/icon on twitch.tv/esl_csgo using Selenium and Python

On twitch.tv/esl_csgo I want to click on the channel points button/icon but it keeps giving me the Error
Message: no such element: Unable to locate element
I have searched and tried various methods of finding the element for over 4 hours and I have not found a way to click on the element
This is my code but it can not click on the button I want to, help would really be appreciated.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time
options = Options()
options.add_argument("--user-data-dir=C:\\Users\\Me\\Desktop\\UserData")
options.page_load_strategy = 'normal'
driver = webdriver.Chrome(options=options)
driver.get("https://twitch.tv/esl_csgo")
time.sleep(10)
element = driver.find_element_by_xpath('//*[#id="c7037441c8fd58e7e0ac6326babcf03d"]/div/div[1]/div/div/div/div/div/section/div/div[5]/div[2]/div[2]/div[1]/div/div/div/div[1]/div[2]/button/div/div/div/div[2]/span')
element.click()
To click on the first channel points button/icon you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[class^='InjectLayout-sc'] > div span[data-test-selector]"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(#class, 'InjectLayout-sc')]/div//span[#data-test-selector]"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Python Selenium, help me locate an element in a website

I want to click on "new order" icon in mt4 web terminal using selenium module in python
This is the code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('./chromedriver')
driver.get("https://www.mql5.com/en/trading")
new_order = driver.find_element_by_xpath('/html/body/div[3]/div[1]/a[1]/span[1]')
new_order.click()
And this is the error that I get:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[3]/div[1]/a[1]/span[1]"}
(Session info: chrome=86.0.4240.198)
What is the correct way to locate that button, I searched and found some ways to locate elements for selenium but I couldn't get any of them work for me.
Looks like your page is dealing with iframes. So while the above answer has good practices, you also need to switch to the iframe:
driver.switch_to.iframe(self,frame reference)
Look for more details at https://www.techbeamers.com/switch-between-iframes-selenium-python/ or https://stackoverflow.com/a/24286392/1387701
The element with tooltip as New Order is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use the following xpath based Locator Strategies:
driver.get('https://www.mql5.com/en/trading')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='webTerminalHost']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Connect to an Account']//following-sibling::div[1]/span"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#title='New Order']/span"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
You can use a different xpath:
new_order = driver.find_element_by_xpath('//a[#title="New Order"]')
But I would suggest By, WebDriverWait, and expected_conditions:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome('./chromedriver')
driver.get("https://www.mql5.com/en/trading")
time.sleep(5)
iframe = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//iframe[#id="webTerminalHost"]')))
driver.switch_to.frame(iframe)
new_order = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//a[#title="New Order"]')))
new_order.click()

Selenium is not able to find the Name field in Twitter signup page

driver = webdriver.Chrome()
driver.get("https://twitter.com/i/flow/signup")
driver.implicitly_wait(10)
setname = driver.find_element_by_name("name")
setname.click()
setname.send_keys("NAME SURNAME")
driver.implicitly_wait(10)
emailoption = driver.find_element_by_xpath("/html/body/div/div/div/div[2]/main/div/div/div/div[2]/div[2]/div/div[4]") #selenium can not find the element XPATH (I found it manually)
emailoption.click() #need to click in the element :)
driver.close()
Selenium can not find the element XPATH (I found it manually) ---Use email instead---.
Tried to find this element other ways... same result.
Maybe there is another way to click in it?
Add a Selenium expected condition (element_to_be_clickable) to your code and use relative XPath. To input your name, click on the link and input your email, you can use :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#autocomplete='name']"))).send_keys('name')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#autocomplete='tel']/following::span[1]']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#autocomplete='email']"))).send_keys('email')
Be sure to add the following imports :
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
If it still fails you can use Javascript :
name = "your_name"
email = "your_email"
elem = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#autocomplete='name']")))
self.driver.execute_script("arguments[0].setAttribute('value', '" + name +"')", elem)
elem2 = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#autocomplete='tel']/following::span[1]']")))
self.driver.execute_script("arguments[0].click();", elem2)
elem3 = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#autocomplete='email']")))
self.driver.execute_script("arguments[0].setAttribute('value', '" + email +"')", elem3)
The elements within Twitter Signup page are React elements. So to send a character sequence to the Name field you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://twitter.com/i/flow/signup")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='name']"))).send_keys("Manoel Augusto")
Using XPATH:
driver.get("https://twitter.com/i/flow/signup")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='name']"))).send_keys("Manoel Augusto")
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
Reference
You can find a detailed relevant discussion in:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element error sending text to Email field in twitter with Selenium Python
Try below code -
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Chrome()
action = ActionChains(driver)
wait = WebDriverWait(driver, 20)
driver.get('https://twitter.com/i/flow/signup')
time.sleep(5) # Add wait here so that twitter login page will load.
NameElement = driver.find_element_by_xpath("//input[#name='name']")
action.move_to_element(NameElement).click().perform()
NameElement.send_keys("Hello")
EmailOption = driver.find_element_by_xpath('//span[text()="Use email instead"]/parent::div')
EmailOption.click()
driver.close()
I ran your code on my computer and It worked perfectly, finding the email option and clicking on it. I also found the same xpath you provided, have you tried running your program in a debugger, and What IDE/text editor are you using? Also, are you leaving the window open when you are running your program? If you close the window it will throw an error. If you have any more details on what is happening when you run your code let me know I'd love to help.
Try below code :
wait = WebDriverWait(driver, 10)
driver.get("https://twitter.com/i/flow/signup")
driver.implicitly_wait(10)
setname = driver.find_element_by_name("name")
setname.click()
setname.send_keys("NAME SURNAME")
driver.implicitly_wait(10)
element1 = wait.until(EC.element_to_be_clickable((By.XPATH, "//body//div[4]")))
element1.click()
Output::

How to locate the username and password field within Instagram login page using Chromedriver and Selenium Python

Here's inspected source code
input aria-label="Phone number, username, or email" aria-required="true" autocapitalize="off" autocorrect="off" maxlength="75" name="username" type="text" class="_2hvTZ pexuQ zyHYP" value=""
I have tried this code run
driver = webdriver.Chrome()
driver.get('https://www.instagram.com/')
driver.find_element_by_xpath("//input[#name=\"username\"]").send_keys(username)
driver.find_element_by_xpath("//input[#name=\"password\"]").send_keys(pw)
driver.find_element_by_xpath('//button[#type="submit"]').click()
But having error like this
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[#name="username"]"}
(Session info: chrome=83.0.4103.61)
My chromedriver and chrome version are match, and finding elements by following instruction. Why am I getting this error?
Instagram application is built through React elements. Hence just after invoking the url when you initiate the search for the login element, you face NoSuchElementException
Solution
To login within Instagram using a valid set of credentials you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategy:
Using XPATH:
driver.get("https://www.instagram.com/")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='username']"))).send_keys("username")
driver.find_element_by_xpath("//input[#name='password']").send_keys("password")
driver.find_element_by_xpath("//button/div[text()='Log In']").click()
Note : You have to add the following imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
Reference
You can find a couple of relevant discussions in:
Filling in login forms in Instagram using selenium and webdriver (chrome) python OSX
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
Observe that while open instagram homepage, it shows spinner on login form for few moment and then display the fields. So your need to manage synchronization in your script.
Use explicit wait in your code until desired field get ready for interaction.
username = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[#name='username']")))
username.send_keys('username')
password = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[#name='password']")))
password.send_keys('pw')
Need to import below packages
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Try the below code:
driver = webdriver.Chrome()
driver.get('https://www.instagram.com/')
txt_user = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, 'username')))
txt_user.send_keys('yourUserName')
txt_pwd = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, 'password')))
txt_pwd.send_keys('yourPassword')
btn_submit = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[type="submit"]')))
btn_submit.click()
Following import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get('https://www.instagram.com/')
element = WebDriverWait(driver, 2).until(
EC.presence_of_element_located((By.ID, "//input[#name=\"username\"]"))
)
element.sendkeys('user')

Categories

Resources