0
votes

issue: i cannot click 'zgadzam sie'. error occurs "selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//span[@class='RveJvd snByac' and text()='Zgadzam się']"}"

question: how can i handle it? image english image

from selenium import webdriver
import time

driver= webdriver.Chrome()
driver.implicitly_wait(3)
driver.get("https://www.google.com/")
driver.find_element_by_xpath("//span[@class='RveJvd snByac' and text()='Zgadzam się']").click()
driver.quit()

time.sleep(5)
2

2 Answers

1
votes

You are sleeping after you are attempting to click the button. You are also quitting the driver before sleeping which is another problem.

Consider using the WebDriverWait().until() function to ensure that the element is loaded instead of relying on some arbitrary amount of time:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, "//span[@class='RveJvd snByac' and text()='Zgadzam się']"))
)

driver.find_element_by_xpath("//span[@class='RveJvd snByac' and text()='Zgadzam się']").click()


The reason why you are getting this error is because the element is nested in an <iframe>. This can be resolved by waiting for the iframe to appear, then waiting for the button to be loaded, then finally clicking the button:

# Wait for the iFrame to be available to switch to it
WebDriverWait(driver, 10).until(
    EC.frame_to_be_available_and_switch_to_it(0)
)

# Wait for the button to be available within that iframe
WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, "//span[@class='RveJvd snByac' and text()='Zgadzam się']"))
)

# Finally click the button
driver.find_element_by_xpath("//span[@class='RveJvd snByac' and text()='Zgadzam się']").click()
0
votes

Try using the full xpath. You can find it by using an xpath tool usually downloaded as a browser extension.