Issue
I've already tried in several ways to click this checkbox with selenium and I couldn't. Link: http://buscatextual.cnpq.br/buscatextual/email.do?metodo=apresentar&seqIdPessoa=246740&nomeDestinatario=Maria_Jos%E9_Panichi_Vieira I already tried with XPATH, with CLASS_NAME and others, but the return is always the same:
no such element: Unable to locate element:
Can anyone help me?
Code trials:
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from fake_useragent import UserAgent
from capmonster_python import RecaptchaV2Task
class ChromeAuto:
def __init__(self):
options = Options()
ua = UserAgent()
self.userAgent = ua.random
print(self.userAgent)
options.add_argument(f'user-agent={self.userAgent}')
self.driver_path = r'chromedriver'
self.options = webdriver.ChromeOptions()
self.options.add_argument('--profile-directory=1')
self.options.add_experimental_option("excludeSwitches", ["enable-automation"])
self.options.add_experimental_option("useAutomationExtension", False)
self.captcha = RecaptchaV2Task("c6eea325a7a78273c062d2bb23a2a43d")
self.chrome = webdriver.Chrome(
self.driver_path,
options=self.options
)
def test(self):
self.chrome.get('http://buscatextual.cnpq.br/buscatextual/email.do?metodo=apresentar&seqIdPessoa=246740&nomeDestinatario=Maria_Jos%E9_Panichi_Vieira')
sleep(5)
self.chrome.find_element(By.XPATH, '//*[@id="recaptcha-anchor"]/div[1]').click()
if __name__ == '__main__':
chrome = ChromeAuto()
chrome.test()
Solution
The ReCaptcha checkbox is within an <iframe>
so 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('http://buscatextual.cnpq.br/buscatextual/email.do?metodo=apresentar&seqIdPessoa=246740&nomeDestinatario=Maria_Jos%E9_Panichi_Vieira') WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='reCAPTCHA']"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.recaptcha-checkbox-border"))).click()
Using XPATH:
driver.get('http://buscatextual.cnpq.br/buscatextual/email.do?metodo=apresentar&seqIdPessoa=246740&nomeDestinatario=Maria_Jos%E9_Panichi_Vieira') WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@title='reCAPTCHA']"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.recaptcha-checkbox-border"))).click()
PS: Clicking on the ReCaptcha checkbox opens the image selection panel.
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 couple of relevant discussions in:
- Switch to an iframe through Selenium and python
- 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
Answered By - undetected Selenium Answer Checked By - Candace Johnson (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.