Python3 - Selenium unable to find xpath provided - python

I am using Python 3 and Selenium to grab some image links from a website as below:
import sys
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.proxy import Proxy, ProxyType
chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('https://www.sky.com/tv-guide/20200605/4101-1/107/Efe2-364')
link_xpath = '/html/body/main/div/div[2]/div[2]/div/div/div[2]/div/div[2]/div[1]/div/div/div[2]/div/img'
link_path = driver.find_element_by_xpath(link_xpath).text
print(link_path)
driver.quit()
When parsing this URL you can see the image in question in the middle of the page. When you right click in Google Chrome and inspect element, you can then right click the element itself within Chrome Dev Tools and get the xpath for this image.
All looks in order to me, however when running the above code I get the following error:
Traceback (most recent call last):
File "G:\folder\folder\testfilepy", line 16, in <module>
link_path = driver.find_element_by_xpath(link_xpath).text
File "G:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "G:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "G:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "G:\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/main/div/div[2]/div[2]/div/div/div[2]/div/div[2]/div[1]/div/div/div[2]/div/img"}
(Session info: headless chrome=83.0.4103.61)
Can anyone tell me why Selenium is unable to find the xpath provided?

To extract the src attribute of the image you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--headless')
options.add_argument('--window-size=1920,1080')
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get('https://www.sky.com/tv-guide/20200605/4101-1/107/Efe2-364')
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.o-layout__item div.c-bezel.programme-content__image>img"))).get_attribute("src"))
Using XPATH:
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--headless')
options.add_argument('--window-size=1920,1080')
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get('https://www.sky.com/tv-guide/20200605/4101-1/107/Efe2-364')
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='o-layout__item']//div[#class='c-bezel programme-content__image']/img"))).get_attribute("src"))
Console Output:
https://images.metadata.sky.com/pd-image/251eeec2-acb3-4733-891b-60f10f2cc28c/16-9/640
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
Reference
You can find a couple of detailed discussion 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

You have the correct xpath, but don't use absolute paths, it's very vulnerable to change. Try this relative xpath : //div[#class="c-bezel programme-content__image"]//img.
And to achieve you mean, please use .get_attribute("src") not .text
driver.get('https://www.sky.com/tv-guide/20200605/4101-1/107/Efe2-364')
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//div[#class="c-bezel programme-content__image"]//img')))
print(element.get_attribute("src"))
driver.quit()
Or better way, use css selector. This should be faster:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.c-bezel.programme-content__image > img')))
Reference : https://selenium-python.readthedocs.io/locating-elements.html

If you are working in headless mode, it usually is a good idea to add window size. Add this line to your options:
chrome_options.add_argument('window-size=1920x1080')

Your xpath seems to be correct. You wasn't able to locate because you forgot to handle the cookie. Try it by yourself. Put the driver on hold for few seconds and click agree to all cookies. And then you will see your element. There are multiple way to handle cookie. I was able to locate xpath by using my own xpath which is cleaner. I visit that element from nearest parent.
Hope this help.

Related

How to fix NoSuchElementException (using XPath expressions with Selenium)

I am trying to have Selenium import metamask. However, when I use the XPath expression /html/body/div[1]/div/div[3]/div/div/div/button, my console returns:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div1/div/div[3]/div/div/div/button"}
(Session info: chrome=93.0.4577.82)
Which is strange as when I use $x("/html/body/div[1]/div/div[3]/div/div/div/button") in Chrome DevTools, it is able to identify the Get Started button. How can I fix this error and why am I getting said error?
Full source code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
options = Options()
options.add_extension('metamask-chrome-9.8.4.crx')
driver = webdriver.Chrome('./chromedriver', options = options)
driver.get('https://google.com')
time.sleep(2)
get_started_button = driver.find_element_by_xpath("/html/body/div[1]/div/div[3]/div/div/div/button")
get_started_button.click()
input('Press [ENTER] to close browsers...')
driver.quit()
Full Error Log:
Traceback (most recent call last):
File "D:\Rias\metamask selenium\script.py", line 13, in <module>
get_started_button = driver.find_element_by_xpath("/html/body/div[1]/div/div[3]/div/div/div/button")
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]/div/div[3]/div/div/div/button"}
(Session info: chrome=93.0.4577.82)
There are four ways to click in Selenium.
I will use this XPath expression:
//button[text()='Get Started']
Code trial 1:
time.sleep(5)
driver.find_element_by_xpath("//button[text()='Get Started']").click()
Code trial 2:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Get Started']"))).click()
Code trial 3:
time.sleep(5)
button = driver.find_element_by_xpath("//button[text()='Get Started']")
driver.execute_script("arguments[0].click();", button)
Code trial 4:
time.sleep(5)
button = driver.find_element_by_xpath("//button[text()='Get Started']")
ActionChains(driver).move_to_element(button).click().perform()
Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
PS: Please check in the dev tools if we have unique entry in HTML DOM or not.
Steps to check:
Press F12 in Chrome → go to element section → do a Ctrl + F → then paste the XPath expression and see, if your desired element is getting highlighted.
When you load the metamask extension to your Selenium driver (Chrome), there's a redirect that happens to the metamask page that you can see through the opened Chrome window.
But Selenium couldn't find the button, because it still sees the first tab as its active tab, so it looks for the button in the wrong place and the wrong tab.
You have to change the active tab in Selenium to be the metamask tab. See the below snippet:
from selenium.webdriver.chrome.options import Options
import time
options = Options()
options.add_extension('metamask-chrome-9.8.4.crx')
driver = webdriver.Chrome('./chromedriver', options = options)
driver.get('https://google.com')
time.sleep(2)
driver.switch_to.window(driver.window_handles[0])
get_started_button = driver.find_element_by_class_name("first-time-flow__button")
get_started_button.click()
input('Press [ENTER] to close browsers...')
driver.quit()
I just added this line:
driver.switch_to.window(driver.window_handles[0])
to switch to the extension page and changed selecting the button by XPath to be by class_name.
It works fine with me.

Automate login with selenium and python, how to correctly extract element for user and password

I am trying to automate the login of a bunch of users to the fitbit site to download their data using the python's module selenium. Unfortunately I do not fully understand by looking at the source html code of the page, how to select the right elements.
My code looking something like this:
driver = webdriver.Firefox()
driver.get("https://accounts.fitbit.com/login")
driver.find_element_by_id("email").send_keys('name.surname#gmail.com')
driver.find_element_by_id("pass").send_keys("myfakepass")
driver.find_element_by_id("loginbutton").click()
But I do not know how to extract the element since driver.find_element_by_id("email") returns the error:
>>> driver.find_element_by_id("email")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 360, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [id="email"]
This makes totally sense to me since if I check the source page of https://accounts.fitbit.com/login I cannot detect any field called "email". But, probably due to my lack of experience, I cannot detect in the html source any of the elements that I need for the login.
Anyone could help me?
Thanks
Your locator seems wrong. Try with below xpath.
Induce WebDriverWait() and wait for element_to_be_clickable()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#type='email']"))).send_keys('name.surname#gmail.com')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#type='password']"))).send_keys("myfakepass")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Login']"))).click()
You need to import below libraries.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
There is a code that works.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome('your path')
url = "https://accounts.fitbit.com/login"
driver.get(url)
time.sleep(2)
email = driver.find_element_by_id("ember660")
email.send_keys("Edward#elric.com")
password = driver.find_element_by_id("ember661")
password.send_keys("Password")
login_button = driver.find_element_by_id("ember701").click()
Your program didn't work because of the id which are wrong
I came up with a simple code that works.
It has been very useful to use the "Inspect" command integrated in chrome for finding the id or the xpath relative to each field. Also the package time was needed to avoid errors.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome('path to chromedriver')
url = "https://accounts.fitbit.com/login"
driver.get(url)
time.sleep(2)
email = driver.find_element_by_id("ember659")
email.send_keys("mail#gmail.com")
password = driver.find_element_by_id("ember660")
password.send_keys('actualpassword')
driver.find_element_by_xpath('//*[#id="loginForm"]/div[4]/div').click()

How to solve a StaleElementReferenceException: Message: stale element reference: element is not attached error using Selenium Ubuntu EC2 using Python

I am trying to extract all data of column "Nb B" in this page : https://www.coteur.com/cotes-foot.php
When I run my code from a Ubuntu PC It works perfectly but when I tried with EC2 Ubuntu It does not give me the good return.
Here is my server : ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-20200611 (ami-0a63f96e85105c6d3)
Here is the python script :
#!/usr/bin/python3
# -*- coding: utf­-8 ­-*-
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
options.add_argument("window-size=1400,800")
options.add_argument("--no-sandbox")
options.add_argument("--disable-gpu")
driver = webdriver.Chrome(options=options)
driver.get('https://www.coteur.com/cotes-foot.php')
#Store url associated with the soccer games
url_links = []
for i in driver.find_elements_by_xpath('//a[contains(#href, "match/cotes-")]'):
url_links.append(i.get_attribute('href'))
print(len(url_links), '\n')
nb_bookies = []
for i in driver.find_elements_by_xpath('//tr[#id and #role="row" ]/td[last()]'):
nb_bookies.append(i.text)
print(nb_bookies)
Here is the output :
Traceback (most recent call last):
File "./coteurchrome.py", line 25, in <module>
url_links.append(i.get_attribute('href'))
File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 141, in get_attribute
self, name)
File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 636, in execute_script
'args': converted_args})['value']
File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: headless chrome=83.0.4103.116)
To print the value of the href attributes you have to induce WebDriverWait for the visibility_of_all_elements_located() and you can use either of the following Locator Strategies:
Using XPATH:
driver.get("https://www.coteur.com/cotes-foot.php")
print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, "//a[contains(#href, 'match/cotes-')]")))])
Using CSS_SELECTOR:
driver.get("https://www.coteur.com/cotes-foot.php")
print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "a[href*='match/cotes-']")))])
Console Output:
['https://www.coteur.com/match/cotes-millwall-blackburn-rid1109557.html', 'https://www.coteur.com/match/cotes-cardiff-derby-county-rid1109558.html', 'https://www.coteur.com/match/cotes-sheffield-w-huddersfield-rid1109559.html', 'https://www.coteur.com/match/cotes-luton-queens-park-rangers-rid1109563.html', 'https://www.coteur.com/match/cotes-chelsea-norwich-rid1109746.html', 'https://www.coteur.com/match/cotes-atalanta-brescia-rid1109694.html', 'https://www.coteur.com/match/cotes-gil-vicente-tondela-rid1109683.html', 'https://www.coteur.com/match/cotes-benfica-vitoria-guimaraes-rid1109899.html', 'https://www.coteur.com/match/cotes-orlando-city-new-york-city-fc-rid1109292.html', 'https://www.coteur.com/match/cotes-philadelphia-union-inter-miami-cf-rid1109809.html', 'https://www.coteur.com/match/cotes-ural-ekaterinburg-arsenal-tula-rid1109831.html', 'https://www.coteur.com/match/cotes-spartak-moscou-terek-akhmat-grozni-rid1109833.html', 'https://www.coteur.com/match/cotes-krylya-sovetov-fc-krasnodar-rid1109834.html', 'https://www.coteur.com/match/cotes-moreirense-pacos-ferreira-rid1109682.html', 'https://www.coteur.com/match/cotes-brentford-preston-rid1109561.html', 'https://www.coteur.com/match/cotes-bristol-c-stoke-rid1109568.html', 'https://www.coteur.com/match/cotes-fk-bodo-glimt-kristiansund-bk-rid1109735.html', 'https://www.coteur.com/match/cotes-molde-viking-rid1109736.html', 'https://www.coteur.com/match/cotes-sandefjord-aalesund-rid1109737.html', 'https://www.coteur.com/match/cotes-stromsgodset-mjondalen-rid1109738.html', 'https://www.coteur.com/match/cotes-valerenga-fk-haugesund-rid1109740.html', 'https://www.coteur.com/match/cotes-shakhtar-donetsk-kolos-kovalivka-rid1109948.html']
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

Python Selenium can't find any element on a webpage

I want to automate a simple task with selenium. Login to this website: https://www.lernsax.de/. I'am trying to locate the element via xpath but that doesn't work at all and I get a NoSuchElementException. I'am using Chromedriver and I have tried to use different locating methods like
find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
but I always get this error. I have already tried different websites and it works fine with xpath.
Any help would mean a lot!
Here's my full code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome(executable_path="C:\chromedriver\chromedriver.exe")
driver.get("https://lernsax.de")
loginbtn = driver.find_element_by_xpath('//*[#id="skeleton_main"]/div[1]/div[2]/div/a')
loginbtn.click()
time.sleep(2)
driver.quit()
and the full error message:
Traceback (most recent call last):
File "C:/Users/.../lernsax.py", line 6, in <module>
loginbtn = driver.find_element_by_xpath('//*[#id="skeleton_main"]/div[1]/div[2]/div/a')
File "C:\Users\...\PycharmProjects\LernsaxAutomation\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\...\PycharmProjects\LernsaxAutomation\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\...\PycharmProjects\LernsaxAutomation\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\...\PycharmProjects\LernsaxAutomation\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="skeleton_main"]/div[1]/div[2]/div/a"}
(Session info: chrome=80.0.3987.149)
An iframe is present on the page, so you need to first switch the driver to the iframe and the operate on the element:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://lernsax.de")
# Switch to iframe
driver.switch_to.frame(driver.find_element_by_id('main_frame'))
# Find the element by applying explicit wait on it and then click on it
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//*[#id='skeleton_main']/div[1]/div[2]/div/a"))).click()
Looks like the login form is contained within an iframe, which you will need to switch into, and perform the operations you need. Add below before you click on login button.
driver.switch_to.frame('main_frame')
As the the desired element is within an <iframe> so to invoke click() on the element you have to:
Induce WebDriverWait for the desired frame_to_be_available_and_switch_to_it().
Induce WebDriverWait for the desired element_to_be_clickable().
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get('https://www.lernsax.de/wws/9.php#/wws/101505.php?sid=97608267430324706358471707170230S5c89c6aa')
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#content-frame")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-qa='reporting-filter-trigger-toggle'][data-ember-action]"))).click()
Using XPATH:
driver.get('https://www.lernsax.de/wws/9.php#/wws/101505.php?sid=97608267430324706358471707170230S5c89c6aa')
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='main_frame']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='mo' and text()='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
Browser Snapshot:
Reference
You can find a coupple of relevant discussions in:
Switch to an iframe through Selenium and python
Ways to deal with #document under iframe

Unable to find `class` on a Google page using selenium and python

I want to push a button on a Google web page, but selenium can't locate it.
Here's how the page appears:
Here's the html:
https://search.google.com/search-console/about
<span class="RveJvd snByac">Start now</span>
Here's the code:
def show_webpage(judge_url):
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get(SITE)
button_element = driver.find_element_by_class_name('RveJvd snByac')
button_element[1].click()
html_source = driver.page_source
driver.close()
return html_source
And this is the error:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such
element: Unable to locate element: {"method":"css
selector","selector":".RveJvd snByac"}
As Micheal said, the find_element_by_class_name takes only one class name at a time as argument. you are passing two. if you want to use two class name then you can use css selector instead as given below.
def show_webpage(judge_url):
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get(SITE)
button_element = driver.find_element_by_css_selector('.RveJvd.snByac')
button_element[1].click()
html_source = driver.page_source
driver.close()
return html_source
Passing multiple classNames within find_element_by_class_name() will result in Invalid selector: Compound class names not permitted using find_element_by_class_name
Moreover, the classnames e.g. RveJvd, snByac, etc looks dynamic.
However, to click on the button with text as Start now on the Google web page https://search.google.com/search-console/about you can use the following Locator Strategy:
Code Block:
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
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("start-maximized")
# chrome_options.add_argument('disable-infobars')
driver = webdriver.Chrome(options=chrome_options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://search.google.com/search-console/about")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Improve your performance on Google Search']//following::div[1]//span[text()='Start now']"))).click()
Most probably these class names are changing each time the page is loaded, you should rather stick to Start now text of the span tag
There is no guarantee that the element will be immediately available in DOM so consider using Explicit Wait to ensure that the document is there
Suggested code change:
driver.get("https://search.google.com/search-console/about")
start_now = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Start now']")))
driver.execute_script("arguments[0].click()", start_now)
More information: How to use Selenium to test web applications using AJAX technology

Categories