Reputation: 51
I am working on a project, wherein I am trying to automate the recaptchas. It has been going good so far, however I have run into an issue. Here is the code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
from selenium import *
from selenium.webdriver import *
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
import requests
def initialize():
options = Options()
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
wait = WebDriverWait(driver, 20)
return driver, wait
def input_info():
name = driver.find_element(By.XPATH, "/html/body/div[3]/div/div/div[1]/form/div[1]/input")
name.send_keys("usernamer12392103821")
password = driver.find_element(By.XPATH, "/html/body/div[3]/div/div/div[1]/form/div[2]/input")
password.send_keys("Password123")
verify_password = driver.find_element(By.XPATH, "/html/body/div[3]/div/div/div[1]/form/div[3]/input")
verify_password.send_keys("Password123")
def select_recaptcha():
men_menu = wait.until(ec.visibility_of_element_located((By.XPATH, "/html/body/div[3]/div/div/div[1]/form/div[7]/div/div/div/iframe")))
men_menu.click()
def get_to_sound():
print("Searching for sound button")
time.sleep(3)
sound_butt_iframe = driver.find_element(By.XPATH, "/html/body/div[5]/div[4]/iframe")
print("Step one complete")
driver.switch_to.frame(sound_butt_iframe)
print("Step two complete")
sound_butt = sound_butt_iframe.find_element(By.XPATH, "/html/body/div/div/div[3]/div[2]/div[1]/div[1]/div[2]/button")
print("Step 4 complete")
driver.switch_to.default_content()
print("Step 5 complete")
sound_butt.click()
### set up driver to go to reddit page!
driver, wait = initialize()
driver.get('https://old.reddit.com/login')
time.sleep(5)
#### Fill in the information necessary! (username, password)
input_info()
time.sleep(3)
print("Finding recaptcha")
select_recaptcha()
print("looking to see if successful!")
try:
successful = wait.until(ec.visibility_of_element_located((By.XPATH, "/html/body/div[5]")))
print("Failed")
print("Call to API with sound file!")
try:
get_to_sound()
except:
print("Failed")
except:
print("Succeeded")
print("Carry on")
time.sleep(10000)
Basically, it will open the https://old.reddit.com/login webpage, enter in a dummy username and password (twice for the confirm) and then click on the recaptcha. This all works great. It realizes that the recaptcha "pops" up, and then it tries to click on the audio button, which is where I run into the problem. I think because it is "stuck" in a new iframe html tag, the XPATH isn't being registered, but I'm not sure. Does someone know how I can get the driver to press the audio control button? I've attached a screenshot below of the html stuff that would be needed for this.
It's the highlighted button tag at the bottom that I am "worried" about
Thanks in advance.
Edit: Here's the picture of the button I am trying to press. I took a picture of the whole recaptcha thing, and the button I am trying to press is the headphones at the bottom.
Upvotes: 0
Views: 139
Reputation: 346
you should get iframe as element with driver.find_element(By.XPATH)
, and then find_element(By.XPATH internal)
against that element
for that use switch_to_frame method
iframe = driver.find_element_by_xpath("//iframe[@title='...']")
driver.switch_to_frame(iframe)
# here access to internal elements by xpath
driver.find_element(By.XPATH, "")
driver.switch_to.default_content()
Upvotes: 1
Reputation: 16
If NoSuchElementException accrued and xpath is right then the scroll to target button :
driver.execute_script('arguments[0].scrollIntoView(true);',
driver.find_element_by_xpath(XPATH OF Target button))
Upvotes: 0