0

I need to find this image element using xpath. Selenium throws a NoSuchElementException yet when the webpage is still open, I can find the element by pasting the xpath into the developer tools find function.

I've tried implicit and explicit wait, normal sleep commands, continually searching for the element in a for loop (8 hours)

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

driver = webdriver.Chrome()
driver.get('https://captcha.com/demos/features/captcha-demo.aspx')

WebDriverWait(driver, 20).until(
  EC.presence_of_element_located((By.XPATH,'//*[@id="c_captchademo_samplecaptcha_CaptchaImage"]')))

img = driver.find_element_by_xpath('//*[@id="c_captchademo_samplecaptcha_CaptchaImage"]')

print(img)
Petya
  • 3
  • 2

1 Answers1

0

You have to Switch to iframe first to access the captcha.Try below code.

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

driver = webdriver.Chrome()
driver.get('https://captcha.com/demos/features/captcha-demo.aspx')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"demo_frame")))
image=WebDriverWait(driver, 20).until(
  EC.element_to_be_clickable((By.XPATH,'//img[@id="c_captchademo_samplecaptcha_CaptchaImage"]')))
print(image.get_attribute('alt'))
KunduK
  • 32,888
  • 5
  • 17
  • 41