简体   繁体   中英

Can't find an element on a frame Selenium Python

I'm trying to make a bot that logs into my account.

After inserting password and username I make the bot clicking on the "I'm not a robot recaptcha" using this code and it works:

def _delay():
      time.sleep(randint(1,3))

frames = driver.find_elements_by_tag_name('iframe') 
driver.switch_to_frame(frames[0])    
recaptcha = driver.find_element_by_xpath('//*[@id="recaptcha-anchor"]/div[1]')
driver.execute_script("arguments[0].click();",recaptcha)  
_delay()

After this it opens up the image recaptcha.

Now, I would like to try using the audio to text method but I can't click on the "audio" button under the images. Here's the code I used:

#finding the frame
driver.switch_to_default_content()
element_image = driver.find_element_by_xpath('/html/body')
element = element_image.find_elements_by_tag_name('iframe')
driver.switch_to_frame(element[0])

_delay()

#clicking on the "audio" button
button = driver.find_element_by_id('recaptcha-audio-button')  #error
driver.execute_script("arguments[0].click();",button)

Here is the output: `Exception has occurred: NoSuchElementException

I have no idea on how to click on the "audio" button. This seems correct to me but still doesn't work. Any tips? `

I suspect that this is happening because your delay() function is finishing before the page element is visible. You could try increasing the length of time it pauses to verify, but the better approach is to refactor this to use Selenium WebDriverWait objects.

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
from selenium.common.exceptions import ElementNotVisibleException, ElementNotSelectableException

driver = webdriver.Firefox() # or chrome...
fluent_wait = WebDriverWait(driver, timeout=10, poll_frequency=1, 
                            ignored_exceptions=[ElementNotVisibleException, 
                                                ElementNotSelectableException])
elem = fluent_wait.until(expected_conditions.element_to_be_clickable((By.ID, "recaptcha-audio-button")))
if elem:
    driver.execute_script("arguments[0].click();",button)
else:
    print("couldn't find button")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM