python: selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator
Question:
I wanted to write a python code on python 3.10 with selenium to automize the loging in my gmail account. There is something false with Xpath.
The Error that I am getting is selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator
Below is my code
from selenium.webdriver.chrome.service import Service
import time
browser = webdriver.Chrome(service=Service("C:/Users/user/Desktop/chromedriver.exe"))
browser.get("https://www.google.de/")
browser.fullscreen_window()
time.sleep(1)
login = browser.find_element("By.XPATH", "//*[@id='gb']/div/div[1]/div/div[1]/a")
login.click()
time.sleep(2)
browser.quit()```
I tried to change the Xpath but it didn't work. I thought that is something false with selenium code then I uninstalled the selenium and installed it again but it didn't work.
Answers:
Newcomer…
Google first. Selenium has been industry standard since I remember, this has been posted in THOUSAND sites.
In short, "By.XPATH"
is pretty wrong. You pass a string there, you has to pass the function.
So, go back to google (or even here- search …), and find how to import By function in selenium (like you did with selenium) then pass the method like login = browser.find_element(By.XPATH, "//*[@id='gb']/div/div[1]/div/div[1]/a")
and you’ll fine.
Remember this after ask very common questions like this, you have THOUSAND of lines examples of everything in google. Spend your time reading.
Remove the double quotes in By.XPATH –
browser.find_element(By.XPATH, "//*[@id='gb']/div/div[1]/div/div[1]/a")
and import By:
from selenium.webdriver.common.by import By
I wanted to write a python code on python 3.10 with selenium to automize the loging in my gmail account. There is something false with Xpath.
The Error that I am getting is selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator
Below is my code
from selenium.webdriver.chrome.service import Service
import time
browser = webdriver.Chrome(service=Service("C:/Users/user/Desktop/chromedriver.exe"))
browser.get("https://www.google.de/")
browser.fullscreen_window()
time.sleep(1)
login = browser.find_element("By.XPATH", "//*[@id='gb']/div/div[1]/div/div[1]/a")
login.click()
time.sleep(2)
browser.quit()```
I tried to change the Xpath but it didn't work. I thought that is something false with selenium code then I uninstalled the selenium and installed it again but it didn't work.
Newcomer…
Google first. Selenium has been industry standard since I remember, this has been posted in THOUSAND sites.
In short, "By.XPATH"
is pretty wrong. You pass a string there, you has to pass the function.
So, go back to google (or even here- search …), and find how to import By function in selenium (like you did with selenium) then pass the method like login = browser.find_element(By.XPATH, "//*[@id='gb']/div/div[1]/div/div[1]/a")
and you’ll fine.
Remember this after ask very common questions like this, you have THOUSAND of lines examples of everything in google. Spend your time reading.
Remove the double quotes in By.XPATH –
browser.find_element(By.XPATH, "//*[@id='gb']/div/div[1]/div/div[1]/a")
and import By:
from selenium.webdriver.common.by import By