I have been really banging my head on this, I can't figure out my problem, if someone can help me it would be appreciated, thank you :).
I'm trying to select with Xpath the twitter search box, from what I know, my input is correct, since on google when I press Ctrl+F and search for it in the "inspect mode" it's highlighting in yellow the search box input tag. But in selenium, I wish it was.
(Using google chrome)
import csv
from getpass import getpass
import time
from time import sleep
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium import webdriver
#other selenium code, working.
twitterS = driver.find_element_by_xpath('//input[contains(#data-testid,"SearchBox")]')
Return
selenium.common.exceptions.NoSuchElementException:
Message: no such element: Unable to locate element:
{"method":"xpath","selector":"//input[contains(#data-testid,"SearchBox")]"}
I tried the relative path, not working either
/html/body/div[1]/div/div/div[2]/main/div/div/div/div[2]/div/div[2]/div/div/div/div[1]/div/div/div/form/div[1]/div/div/div[2]/input
Your XPath seems to be fine (I've checked it). If Selenium tells you the elements is not there it means that it's not there in the moment it tries to find it. I don't see any frame or shadow DOM there so I would say you need to wait for this element before getting it. I would also make sure if at that point you are really on the page you're expecting to see the element at. Without the code I can't really help better.
Related
Disclaimer: I'm coming back to scripting after more than a decade so apologies in advance for such a basic question but help is much needed and appreciated.
I recently tried to find scripts to automate my job hunt, to that end I found a script that would help me login to a job portal and apply to jobs matching my criteria.
But I believe the script is not updated because I'm coming across an error when I'm running it:
NoSuchElementException: no such element: Unable to locate element:
{"method":"xpath","selector":"//*[#id="root"]/div[2]/div[2]/div/form/div[2]/input"}
(Session info: chrome=98.0.4758.109)
I believe this is in regard to the following lines of code:
driver.get("https://www.naukri.com")
driver.find_element_by_xpath('//*[#id="login_Layer"]/div').click()
time.sleep(5)
driver.find_element_by_xpath('//*[#id="root"]/div[2]/div[2]/div/form/div[2]/input').send_keys("YOUR NAUKRI LOGIN ID")
driver.find_element_by_xpath('//*[#id="root"]/div[2]/div[2]/div/form/div[3]/input').send_keys("YOUR NAUKRI PASSWORD")
time.sleep(5)
driver.find_element_by_xpath('//*[#id="root"]/div[2]/div[2]/div/form/div[6]/button').click()
The script is able to go to Naukri.com (the job portal) and find the path for login_Layer ID, which opens the login sidebar. But then it is not able to find the username and password field. Maybe because the webpage has changed.
I'm trying to inspect the page elements and find the right xpath but having no luck. Again, haven't touched XML/HTML or web development of any kind for over a decade (and was a novice to begin with) so finding it hard.
Any guidance or help will be really appreciated.
Looking forward to your answers.
Thanks in advance!
Yeah your XPaths are outdated.
If you are on a chromium browser, right click on the element you want the XPath for and click inspect. If your DevTools are not yet open, do this twice. Then it will highlight your element in the page source. After that do a right click on the highlighted element, copy, and "Copy XPath" to update your script.
driver.find_element_by_xpath('//*[#id="root"]/div[2]/div[2]/div/form/div[2]/input').send_keys("YOUR NAUKRI LOGIN ID")
This syntax should still work, but modern guidelines want you to use this:
from selenium.webdriver.common.by import By
driver.find_element(By.XPATH, '//*[#id="root"]/div[3]/div[2]/div/form/div[2]/input').send_keys("YOUR NAUKRI LOGIN ID")
wait=WebDriverWait(driver,20)
driver.get("https://www.naukri.com")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#login_Layer > div'))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'form > div:nth-child(2) > input'))).send_keys("user")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'form > div:nth-child(3) > input'))).send_keys("pw")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'button.btn-primary.loginButton'))).click()
You'd want to use webdriver waits and switch up your xpaths.
Imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Your XPATHs are actually incorrect.
What I would suggest you do is to write the most resilient XPath possible since the longer the XPath, the more brittle it becomes. Having the XPath based on a big HTML tag hierarchy is usually not a good idea since if there are new tags introduced in between (a new div tag) it will break. Which I believe what happened to this application also.
To avoid this you can use below xpaths.
driver.find_element_by_xpath("//label[text()='Email ID / Username']/../input").send_keys("YOUR NAUKRI LOGIN ID")
driver.find_element_by_xpath("//label[text()='Password']/../input").send_keys("YOUR NAUKRI PASSWORD")
I'm using selenium and I have a problem handling the accept cookies pop-up.
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
web = webdriver.Chrome("C:/chromedriver")
web.get('https://www.spaargids.be/sparen/schuldsaldo-berekenen.html')
time.sleep(2)
akkoord = web.find_element_by_xpath('/html/body/div/div[2]/div[3]/button[1]')
When I inspect the element, I can easily find it. But selenium is unable to do it.
Does anybody know how to fix this?
The problem is that the element is inside an iframe, you need to switch to the respective iframe.
Try this before akkoord variable:
web.switch_to.frame(web.find_element_by_xpath('//iframe[#title="SP Consent Message"]'))
That should make it work, then use the same approach but updating the XPATH to switch to the iframe where the content is.
I'm trying to make an auto bump program for forums but I can't seem to interact with anything.
import webbrowser
import pyautogui
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("https://ogusers.com/member.php?action=login")
#Login
inputElement = driver.find_element_by_class_name("textbox")
inputElement.send_keys("xKyle")
Can't really tell where problem lies, you need to say exactly what is happening, let's start with not giving selenium path to ChromeDriver. You need to download it from here, make sure to get driver compatible with your browser verison.
Next give path to it, if you are on windows path will look like:
r"C:\dir\dir\chromedriver.exe'
you need to give 'r' for raw data, otherwise interpreter will think that you are trying to give some weird in-string command like \n for new line
Also try not to search by class name, for 99% of time you want to use Xpath, especially if it's relative. So just click on element, and copy xpath.
For more answers you need to tell exactly what are you trying to do and when it fails :)
Have searched through some of the previous questions and have not been able to rectify - I'm a complete newbie to this, so please forgive the ignorance... Trying to select the third 'Radio' button on the page using the following:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
import smtplib
import time
date = time.strftime("%d %b %Y" + ", " + "%H:%M%p")
print (time.strftime("%d %b %Y" + ", " + "%H:%M%p"))
driver=webdriver.Chrome()
driver.maximize_window()
driver.get("http://www.water.nsw.gov.au/water-licensing/registers")
driver.implicitly_wait(10)
driver.find_element_by_xpath('// [#id="wizard_tabs"]/div/div[1]/div/ul/li[3]/input').click()
The result is:
"Message: no such element: Unable to locate element:
{"method":"xpath","selector":"//[#id="wizard_tabs"]/div/div[1]/div/ul/li[3]/input"}"
Have tried waiting for longer (500), but it doesn't make a difference. When I 'inspect' the page that has opened, the xpath is still the same. I know there's a simple answer and I'm hoping you internet legends can help!
A few points of order:
There is no point in implicitly waiting when you can just perform an explicit wait (WebdriverWait.until) on a unique element on that page.
If the webdriver says the element isn't there, then the locator you are looking for is not there, at least not where it is actually looking. iframes can make you think you are looking in one place when really the webdriver is looking in another.
Always confirm your locators in the browser the moment you suspect something is off. Or just every time, it will save all that time you spend running your script, launching your browser, and loading the page every time as you tinker. Go to your browser's dev tools, click on the console tab, and type $$("css selector here") or $x("xpath here") and hit enter. If an element is returned, click the arrow to expand it and hover over what displays. Is it highlighting the desired element on the page? Then and ONLY then are you using a good locator.
If the dev tools console says your locator is good, but python still won't find it - even after a WebdriverWait - find the iframe that is stealing your focus and use webdriver.switch_to(iframe_name) to steal your focus back to the iframe with your element.
Practice less-brittle XPATHs. They can be quite surgical and readable with minimal effort. I would be willing to bet your input has something we could use to find it directly. If not, an element right above or below it in the hierarchy likely does. Post the HTML of your desired element and we'll know for sure.
To handle an iframe issue:
iframe_element = driver.find_element_by_css_selector("iframe[name='name_of_the_iframe']")
driver.switch_to.frame(iframe_element)
# Now find your element
I have a python script where i'm using Selenium to do some testing.
Currently I am trying to select some elements in multiple list boxes.
You can check out the site here: http://www.guloggratis.dk/annonce/opret (The language is Danish, if you are wondering)
So what I want to do is select a category, then some new categories pops up and I select one of those until there are no more categories left.
I can select the first element in the first box like this:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.guloggratis.dk/annonce/opret')
browser.find_element_by_css_selector("span.pos-absolute").click()
browser.find_element_by_id("categoryId_597").click()
While this works, I don’t know why i need to do
browser.find_element_by_css_selector("span.pos-absolute").click()
Anyways if I try select a category from the list that appears in the second box like this
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.guloggratis.dk/annonce/opret')
browser.find_element_by_css_selector("span.pos-absolute").click()
browser.find_element_by_id("categoryId_597").click()
browser.find_element_by_id("categoryId_598”).click()
it doesn’t work even though "categoryId_598” is an id of one of the new categories.
I appreciate any help you might provide and I look forward to hear your answer.
If you have a problem reproducing the error, make a comment and I’ll instantly respond from my phone.
Maybe because of the cookie window?
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.guloggratis.dk/annonce/opret')
browser.find_element_by_id("cookieClose").click()
browser.find_element_by_id("categoryId_597").click()
browser.find_element_by_id("categoryId_598").click()
works fine for me.