selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: - python

XPath doesn't work on below path code.
HTML
<span class="voter_details">
<h4 class="v_msg" id="v_msg" style="font-size: 14px;margin-bottom:25px;">Hi, you are here XXXfor in a while, kindly enter your name!</h4>
<input value="" placeholder="Enter your Name" type="text" id="v" name="v" class="form-control name_box" maxlength="20">
</span>
Error
selenium.common.exceptions.NoSuchElementException: Message: no such element:
Unable to locate element: {"method":"xpath","selector":"//*[#id='v']"}
Code
driver.find_element(driver.find_element(By.XPATH, "//*[#id='v']")).send_keys('random.choice(list)')

Use following xpath to access the input element.
driver.find_element_by_xpath("//input[#placeholder='Enter your Name'][#id='v']").send_keys('Ashwani')
OR You can use WebdriverWait to wait for the element to be clickable and send then enter the value.
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//input[#placeholder='Enter your Name'][#id='v']"))).send_keys('Ashwani')
To execute that you need to have 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

To send a character sequence you you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.form-control.name_box#v[placeholder='Enter your Name']"))).send_keys(random.choice(list))
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='form-control name_box' and #id='v'][#placeholder='Enter your Name']"))).send_keys(random.choice(list))
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

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="uname"]"}
I got same error while I was working with Selenium.
The problem is slow internet/big website. Try adding explicit or implicit delay or you may import time module and add time.sleep before clicking/sending keys

Related

Unable to click label that doesn't have any text using Python and Selenium

I'm trying to click the label below.
Label html:
<div class="is-inline-block">
<input id="entradas-checkbox-condiciones" type="checkbox" class="switch is-rounded is-small">
<label style="padding-left:1rem;">
I've tried clicking the label with this code:
WebDriverWait = wait
label1 = wait(self.driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, "//*[#class = 'is-inline-block']")))[3]
label1.find_element_by_xpath('//label[style="padding-left:1rem;"]').click()
But i get the following error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:
Can somebody help me with the code?
Thanks in advance.
To locate and invoke click() on the clickable element instead of presence_of_element_located() 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(self.driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.is-inline-block input.switch.is-rounded.is-small#entradas-checkbox-condiciones"))).click()
Using XPATH:
WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='is-inline-block']//input[#class='switch is-rounded is-small' and #id='entradas-checkbox-condiciones']"))).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
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

Type in a textbox via python: unable to locate element

I'm trying to locate an element using python selenium, and have the html below:
<input class="form-control" type="text" placeholder="University Search">
I couldn't locate where to type what I want to type.
from selenium import webdriver
import time
driver = webdriver.Chrome(executable_path=r"D:\Python\Lib\site-packages\selenium\chromedriver.exe")
driver.get('https://www.topuniversities.com/university-rankings/university-subject-rankings/2020/engineering-technology')
#<input class="form-control" type="text" placeholder="University Search">
text_area = driver.find_element_by_name('University Search')
text_area.send_keys("oxford university")
You are attempting to use find_element_by_name and yet this element has no name attribute defined. You need to look for the element with the specific placeholder attribute you are interested in - you can use find_element_by_xpath for this:
text_area = driver.find_element_by_xpath("//input[#placeholder='University Search']")
Also aside: When I open my browser, I don't see an element with "University Search" in the placeholder, only a search bar with "Site Search" -- but this might be a regional and/or browser difference.
Make sure you wait for the page to load using webdriver waits,click the popup and then proceed to target the element to send keys to.
driver.get('https://www.topuniversities.com/university-rankings/university-subject-rankings/2020/engineering-technology')
driver.maximize_window()
wait=WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='OK, I agree']"))).click()
text_area=wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR ,"td.uni-search.uni.sorting_disabled > div > input")))
text_area.send_keys("oxford university")
Imports
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Element to target
<input class="form-control" type="text" placeholder="University Search">
To send a character sequence to the element you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element_by_css_selector("input.form-control[placeholder='University search']").send_keys("oxford university")
Using xpath:
driver.find_element_by_xpath("//input[#class='form-control' and #placeholder='University search']").send_keys("oxford university")
Ideally, to send a character sequence to the 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:
driver.get("https://www.topuniversities.com/university-rankings/university-subject-rankings/2020/engineering-technology")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.form-control[placeholder='University search']"))).send_keys("oxford university")
Using XPATH:
driver.get("https://www.topuniversities.com/university-rankings/university-subject-rankings/2020/engineering-technology")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='form-control' and #placeholder='University search']"))).send_keys("oxford university")
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
Very close, try the XPath when all else fails:
text_area = driver.find_element_by_xpath("//*[#id='qs-rankings']/thead/tr[3]/td[2]/div/input")
You can copy the full/relative XPath to clipboard if you're inspecting the webpage's html.

NoSuchElementException: Message: no such element: Unable to locate element error locating element with Selenium and Python

I have a program which uses the python selenium webdriver and I get the following runtime error:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[#id='id_login']"}
(Session info: chrome=83.0.4103.1
HTML
<input type="text" name="login" placeholder="Type your username" required="" id="id_login" xpath="1">16)
code:
from selenium import webdriver
#from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path="c:\\Chrome1\chromedriver.exe")
driver.get("https://www.jstor.org")
print(driver.title)
driver.find_element_by_xpath("//a[#class='inline-block plm']").click()
driver.find_element_by_xpath("//input[#id='id_login']").send_keys('xxxxxx#gmail.com')
This error means that selenium could not localize the element because it was not on the site or it did not load. I suggest you using the WebdriverWait() function. It will wait X seconds until the element is clickable. If it will still not be, it will throw an error.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(self.webdriver, 60).until(EC.element_to_be_clickable((By.XPATH, '//a[#class='inline-block plm']')))
More effective if you direct directly to this url:
https://www.jstor.org/action/showLogin?redirectUri=/
And use selenium wait to solve your issue.
driver.get('https://www.jstor.org/action/showLogin?redirectUri=/')
user_name = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='id_login']")))
user_name.send_keys('xxxxxx#gmail.com')
Although your xpath will work, using an id looks better .element_to_be_clickable((By.ID, "id_login"))
You need 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
To send a character sequence to the Username 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://www.jstor.org/")
ebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.inline-block.plm"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#id_login"))).send_keys("SunilTirupathi#stackoverflow.com")
Using XPATH:
driver.get("https://www.jstor.org/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='inline-block plm']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='id_login']"))).send_keys("SunilTirupathi#stackoverflow.com")
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

Selecting xpath - Selenium

I am coding a bot for tinder with selenium but its not working. Here is my code.
fb_button = self.driver.find_element_by_xpath('//*[#id="content"]/div/div[1]/div/div/main/div/div[2]/div[2]/div/div/span/div[2]/button')
fb_button.click()
This code is for clicking the facebook login button. However when i run the code, it dont work healthy.I am getting an error like this.
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate e
lement: {"method":"xpath","selector":"//*[#id="content"]/div/div[1]/div/div/main/div/div[2]/div
[2]/div/div/span/div[2]/button"}
However when i try to create an object and try to call this function, it returns something.
HTML of the button:
<button type="button" class="button Lts($ls-s) Z(0) CenterAlign Mx(a) Cur(p) Tt(u) Bdrs(100px) Px(24px) Px(20px)--s Py(0) Mih(42px)--s Mih(50px)--ml button--outline Bdw(2px) Bds(s) Trsdu($fast) Bdc(#fff) C(#fff) Bdc(#fff):h C(#fff):h Bdc(#fff):f C(#fff):f Bdc(#fff):a C(#fff):a Fw($semibold) focus-button-style W(100%) Fz(4vw)--ms" draggable="false" aria-label="Facebook ile oturum aç"><span class="Pos(r) Z(1) D(ib)">Facebook ile oturum aç</span></button>
you can wait until the element can clickable and present in HTML dom.
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
fb_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "your_xpath")))
fb_button.click()
you can check the wait conditions here!
You can click on the element using xpath:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Facebook ile oturum aç']"))).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

NoSuchElementException: Message: Unable to locate element while trying to locate an element using Selenium and Python

I have a specific login button I can't access with Python Firefox Selenium. It's the login button on this webpage: https://schalter.asvz.ch/tn/lessons/39616
I'm running Ubuntu 16.04, Python 3.5.2, Firefox 65.0 and Selenium 3.141.
I tried several combinations of approaches I found here on stackoverflow including the following:
login = driver.find_element_by_xpath("//*[#class='btn btn-default ng-star-inserted']")
login = driver.find_element_by_xpath("//button[#class='btn btn-default ng-star-inserted']")
login = driver.find_element_by_class_name('btn btn-default ng-star-inserted')
login = driver.find_element_by_xpath("//*[contains(., 'Login')]")
login = driver.find_element_by_name('app-lessons-enrollment-button')
But none of them worked. Always resulting in:
NoSuchElementException: Message: Unable to locate element:
//*[#class='btn btn-default ng-star-inserted']
What is so different with this button? How can I make it work?
This error message...
NoSuchElementException: Message: Unable to locate element: //*[#class='btn btn-default ng-star-inserted']
...implies that the ChromeDriver was unable to locate the desired element through the locator you have used.
Virtually, your first two(2) locators were just perfecto.
However, the desired element is an Angular element so to locate the element you have 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.btn.btn-default.ng-star-inserted[title='Login']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-default ng-star-inserted' and #title='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
Try the below option.
Login=driver.find_element_by_css_selector("button.ng-star-inserted")
Or try this
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'button.ng-star-inserted'))).click()
You need following imports for option 2.
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
Following xpath works just fine (tested using selenium java)
//button[#title='Login']
I was able to locate and click the button.
Try the below xpath:
xpath = "//button[#title='Login']"
element = driver.find_element_by_xpath(xpath);
element.click();

Categories

Resources