Can't find button by xpath (Selenium, Python)

336 views
Skip to first unread message

Michael Ban

unread,
Nov 18, 2022, 5:05:31 AM11/18/22
to Selenium Users
Hi, I'm fairly new to Selenium. I managed to start browser etc. Then I wanted to click "Accept"  cookies button from google.com. Icopied it's xpath from inspect:
xpath is //*[@id="L2AGLb"]

This is what it looks......

browser.find_element(By.XPATH, "//*[@id="L2AGLb"]")
SyntaxError: invalid syntax. Perhaps you forgot a comma?
find_element(By.XPATH, //*[@id="L2AGLb"])
SyntaxError: invalid syntax
find_element(By.XPATH, //*[@id="L2AGLb"])
SyntaxError: invalid syntax
browser.find_element(By.XPATH("//*[@id="L2AGLb"]")
                     
SyntaxError: invalid syntax. Perhaps you forgot a comma?
browser.find_element(By.XPATH(//*[@id="L2AGLb"])
                     
SyntaxError: invalid syntax
browser.find_element(By.XPATH("//*[@id="L2AGLb"]");
                     
SyntaxError: invalid syntax. Perhaps you forgot a comma?
browser.find_element(By.XPATH("//*[@id="L2AGLb"]"));
                     
SyntaxError: invalid syntax. Perhaps you forgot a comma?
dateBox = driver.find_element_by_xpath("//*[@id="L2AGLb"]")
                     
SyntaxError: invalid syntax. Perhaps you forgot a comma?
dateBox = browser.find_element_by_xpath("//*[@id="L2AGLb"]")
                     
SyntaxError: invalid syntax. Perhaps you forgot a comma?
browser.find_element_by_xpath("//*[@id="L2AGLb"]")
                     
SyntaxError: invalid syntax. Perhaps you forgot a comma?


I tried like everything and have no idea what's wrong 🙄

21stcentury teevee

unread,
Nov 28, 2022, 1:07:01 AM11/28/22
to Selenium Users
Try

driver.find_element(By.XPATH, '//*[@id="L2AGLb"]')
or
browser.find_element(By.XPATH, '//*[@id="L2AGLb"]')

I think if the XPATH contains a pair of quotation marks then you need to use single quotes for the outer pair.
Reply all
Reply to author
Forward
0 new messages