How to interact with the reCAPTCHA audio element using Selenium and Python - python

I want to, click on the button to resolve the captcha through the audio, but selenium does not detect the specified "id".
browser.get("https://www.google.com/recaptcha/api2/demo")
mainWin = browser.current_window_handle
iframe = browser.find_elements_by_tag_name("iframe")[0]
browser.switch_to_frame(iframe)
CheckBox = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID ,"recaptcha-anchor"))).click()
sleep(4)
audio = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID ,"recaptcha-audio-button"))).click()

To click() on the button to resolve the captcha through the audio as the desired elements are 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 the following Locator Strategies:
Code Block:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://www.google.com/recaptcha/api2/demo")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://www.google.com/recaptcha/api2/anchor']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span#recaptcha-anchor"))).click()
driver.switch_to.default_content()
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='recaptcha challenge']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#recaptcha-audio-button"))).click()
Browser Snapshot:
Reference
Ways to deal with #document under iframe
Outro
You can find a couple of relevant discussions in:
How to click on the reCaptcha using Selenium and Java
CSS selector for reCaptcha checkbok using Selenium and vba excel
Find the reCAPTCHA element and click on it — Python + Selenium

Very useful, just put your attention, that text: 'recaptcha challenge' in selector below depends from regional settings/language:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='recaptcha challenge']")))

Related

Unable to click button Selenium Python

I am web-scraping reviews from Goodreads for a project. Here's an example of a page I've been trying: https://www.goodreads.com/book/show/2767052-the-hunger-games/reviews?
The reviews page initially shows 30 reviews with a 'Show More' button at the bottom. Selenium seems unable to click the button.
Here is the code I'm using:
showmore_button = driver.find_element(By.XPATH, '/html/body/div[1]/div/main/div[1]/div[2]/div[4]/div[4]/div/button/span[1]')
driver.execute_script("arguments[0].click();", showmore_button)
I have also tried
showmore_button.click()
but that leads to an exception stating that the element is not clickable
For more context my driver is set up like this:
def createdriver():
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument("start-maximized")
options.add_argument('--window-size=1920,1080')
options.add_argument("--incognito")
driver = webdriver.Chrome(options=options)
return driver
and then I use:
driver = createdriver()
driver.get(url)
Where the URL is the reviews page I'm trying to scrape
To click on the element Show more reviews at the bottom of the page you need to scrollIntoView() inducing WebDriverWait for the visibility_of_element_located() and you can use the following locator strategies:
Code block:
driver.get('https://www.goodreads.com/book/show/2767052-the-hunger-games/reviews?')
time.sleep(5)
driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='ReviewsList__listContext ReviewsList__listContext--centered']//span[contains(., 'Displaying 1 -')]"))))
driver.execute_script("arguments[0].click();", driver.find_element(By.XPATH, "//span[text()='Show more reviews']"))
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:
As the page loads for few milli-seconds before the element is displayed, you need to apply selenium waits. Try using implicit wait after creating the driver instance, see code below:
driver = createdriver()
driver.implicitly_wait(10)
driver.get(url)
Above code waits for 10 seconds searching for the element before throwing error
Also another suggestion:
Instead of using an absolute XPath, as a best practice use relative XPath. This is because relative XPath is more consistent compared to absolute XPath. Absolute XPath may stop working, If the DOM structure changes in the future. Try the below relative XPath:
showmore_button = driver.find_element(By.XPATH, '//span[contains(text(),"Show more reviews")]')
driver.execute_script("arguments[0].click();", showmore_button)

How to navigate past the popup ad using Selenium

I'm trying to navigate through oddschecker website using selenium in python with chrome webdriver but an add comes up. See image:
Is there a line of code I can use to get rid of the ad and access the site without having to click on the cross in top right corner manually?
Code trials:
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
mylink = 'https://www.oddschecker.com'
driver.get(mylink)
Any help would be greatly appreciated.
To navigate past the popup ad the only way is to click and close the popup ad element inducing WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
driver.get("https://www.oddschecker.com/")
# click and close the cookie banner
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[class^='CookieBannerAcceptButton']"))).click()
# click and close the popup ad
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='Close Offers Modal'][class^='CloseButtonDesktop']"))).click()
Using XPATH:
driver.get("https://www.oddschecker.com/")
# click and close the cookie banner
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='OK']"))).click()
# click and close the popup ad
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#aria-label='Close Offers Modal' and starts-with(#class, 'CloseButtonDesktop')]"))).click()
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
This would help you in closing the ad.
driver.get('https://www.oddschecker.com')
time.sleep(4)
driver.find_element(By.XPATH, "//*[#aria-label='Close Offers Modal']").click()
P.S. I wrote in python. If you are writing in some other language, you may transform this line per the syntax of your language.
You can integrate AdBlocker extension within you chrome driver just like this:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('load-extension=' + EXTENSION_PATH)
driver = webdriver.Chrome(DRIVER,chrome_options=chrome_options)
driver.create_options()

How to click on 2FA 'Duo Push Button' using Selenium in python

I am trying to click on a 2FA 'Send me a push' button, however none of my previous attempts have succeeded
def launch_login():
#open login web page
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("http://my.csus.edu")
#enter login
driver.find_element(By.XPATH, "//input[#id='username']").send_keys("xxxxxxxx")
driver.find_element(By.XPATH, "//input[#id='password']").send_keys("xxxxxxxx")
login = driver.find_element(By.XPATH, "//button[contains(text(),'Login')]")
login.click()
#click duo push button
driver.find_element(By.XPATH, "//button[starts-with(#class, 'positive auth-button') and contains(., 'Send Me a Push')]").click()
return driver
driver = launch_csus()
This is error I am receiving
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button[starts-with(#class, 'positive auth-button') and contains(., 'Send Me a Push')]"}
(Session info: chrome=101.0.4951.41)
I am still pretty new to selenium and am learning as I go. Is the button a special type of button that behaves in a certain way? Is there a specific way to access the button?
Send me a push inspect:
The button with the text as Send Me a Push is within an <iframe>
Solution
So to click on Send Me a Push 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:
driver.get("http://my.csus.edu")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='username']"))).send_keys("xxxxxxx")
driver.find_element(By.XPATH, "//input[#id='password']").send_keys("xxxxxxxx")
driver.find_element(By.XPATH, "//button[contains(text(),'Login')]").click()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='duo_iframe']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[normalize-space()='Send Me a Push']"))).click()
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:

Unable to locate element within an iframe while the element actually exists

No matter how long I wait, it seems selenium can't find the "watch_online" button.
I've tried both by XPath, full XPath, and CSS selector.
I want to get the href link from the "Watch Online" button.
import os
import glob
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
# Create local browser cache
browser_data_location = "browser_data"
options = webdriver.ChromeOptions()
options.add_argument(f'user-data-dir={os.getcwd()}/{browser_data_location}')
i_m_not_a_robot_xpath = '//*[#id="landing"]/div[2]/center/img'
generate_link_xpath = '//*[#id="generater"]/img'
click_to_continue = '//*[#id="showlink"]'
get_download_link = '/html/body/section/div/div/div/center/a'
watch_online = '//*[#id="download-hidden"]/a'
with webdriver.Chrome(options=options, ) as driver:
wait = WebDriverWait(driver, 10)
time.sleep(2)
driver.get(
"https://www.rtilinks.com/?82255aba71=RmwzVDZObDFBdDQvay8zRjhiaStoM004Ymd1T201MnBQelJpdW5oK1UxeGFvbFZUY1FEVXMrY0o2UnhqeGxOOFlwN3JlUElad2h0ek9pQ1ZFZndXSG9UTzA1aFpmTEhoanBVUldEYWwwWVU9")
# wait.until(ec.element_to_be_clickable((By.CSS_SELECTOR, upload_box_css))).send_keys(file)
wait.until(ec.element_to_be_clickable((By.XPATH, i_m_not_a_robot_xpath))).click()
# time.sleep(1)
wait.until(ec.element_to_be_clickable((By.XPATH, generate_link_xpath))).click()
wait.until(ec.element_to_be_clickable((By.XPATH, click_to_continue))).click()
# original_window = driver.current_window_handle
driver.close()
driver.switch_to.window(driver.window_handles[0])
wait.until(ec.element_to_be_clickable((By.XPATH, get_download_link))).click()
time.sleep(2)
link = driver.find_element(By.XPATH, watch_online)
print(link.get_attribute('href'))
The element Watch Online 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 PARTIAL_LINK_TEXT:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(#src, 'https://purefiles.in')]")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Watch Online"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://purefiles.in']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.button.is-success"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='button is-success' and contains(., 'Watch Online')]"))).click()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(#src, 'https://purefiles.in')]")))
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
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

How do I type login credentials using Selenium and Python within kingdoms.com?

I tried a few websites don't have a problem. "baseURL" tried a lot of time.
driver.find the class element
can't get. Anyone can help?
Click this link below and inside the login page ...
which one to use ??
Code trials:
PATH = "D:\chromedriver.exe"
driver = webdriver.Chrome(PATH)
baseURL = "https://www.kingdoms.com/#logout"
driver = get(baseURL)
print(driver.title)
driver.find_element_by_xpath
There is an nested iframe in that page, so you have to switch to iframe and again to iframe, and then you can send the keys to email address input field.
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
#driver.implicitly_wait(50)
driver.get("https://www.kingdoms.com/#logout")
wait = WebDriverWait(driver, 20)
try:
wait.until(EC.element_to_be_clickable((By.ID, "cmpbntyestxt"))).click()
except:
pass
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe.mellon-iframe")))
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[src*='https://mellon-t5.traviangames.com/account/logout']")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='email']"))).send_keys('Mario#gmail.com')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='password']"))).send_keys("marios password")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='submit']"))).click()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
The desired fields are within nested <iframe> elements so you have to:
Induce WebDriverWait for the parent frame to be available and switch to it.
Induce WebDriverWait for the child 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('https://www.kingdoms.com/#logout')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.cmpboxbtn.cmpboxbtnyes]"))).click()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.mellon-iframe")))
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://mellon-t5.traviangames.com/account/logout/applicationDomain']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='email']"))).send_keys("Mario#Hooi.com")
driver.find_element_by_css_selector("input[name='password']").send_keys("MarioHooi")
Using XPATH:
driver.get('https://www.kingdoms.com/#logout')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='cmpboxbtn cmpboxbtnyes']"))).click()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#class='mellon-iframe']")))
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(#src, 'https://mellon-t5.traviangames.com/account/logout/applicationDomain')]")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='email']"))).send_keys("Mario#Hooi.com")
driver.find_element_by_xpath("//input[#name='password']").send_keys("MarioHooi")
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
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python
How To sign in to Applemusic With Python Using Chrome Driver With Selenium

Categories