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!
CodePudding user response:
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")
CodePudding user response:
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
CodePudding user response:
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")