-1

I want to push a button on a Google web page, but selenium can't locate it.

Here's how the page appears: enter image description here

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"}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
rachelvsamuel
  • 1,551
  • 2
  • 10
  • 21

3 Answers3

1

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
Murthi
  • 5,299
  • 1
  • 10
  • 15
1

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()
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1
  1. 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
  2. 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

Dmitri T
  • 159,985
  • 5
  • 83
  • 133