I am trying to use selenium to get a particular attribute from recaptcha audio-source.
however, i'm unsure of how to do so. Here is an example from https://www.google.com/recaptcha/api2/demo
Click on "I am not a robot"
Select the headphone
Extract the src link
<audio id="audio-source" src="https://www.google.com:443/recaptcha/api2/payload?p=06AGdBq278w_OvG1dn_-_sgoVrqxLWcBq0IBkj2htJcsS-iTT3HtmwlhcTfBrcbQelxGI0hiep-082RypK_wZUTE-XzVbmcJ8zANM9l5O_0ka3x_7E_Hf_-vGqcRHCdRO7w2krqcgZDJSu1wj5wVyWhbDGITl55YsOs21NoX4aHk38173DPPu-Kj6T3mnqnA_3rMsdTkOUtMyl&k=6Le-wvkSAAAAAPBMRTvw0Q4Muexq9bi0DJwx_mJ-" style="display: none"></audio>
I want to retrieve the src link and print it out
may I know if there is any ways for me to use selenium to do so?
My code so far allows me to load into recaptcha demo page -> Click on I am not a bot -> click on the audio button
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
PATH="C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("http://localhost/recaptcha-v2/")
# 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()
#This works, i can get the captcha token
# Src_URL = driver.find_element_by_id('recaptcha-token').get_attribute('value')
#This does not work, it can't locate the src
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "audio-source")))
Src_URL = driver.find_element_by_id('audio-source').get_attribute('src')
print(Src_URL)
Please advise thank you!
I have used the below code to extract src attribute from the audio tag (Change iframe if the audio tag is in another iframe) -
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver import ActionChains
import time
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 5)
action = ActionChains(driver)
driver.get("YourURL")
# you can also use time.sleep(5)
wait.until(expected_conditions.presence_of_element_located((By.ID, "audio-source")))
Src_URL = driver.find_element_by_id('audio-source').get_attribute('src')
print(Src_URL)
Related
I'm new to Python. To enter the Tradingview.com site with Selenium library.
I wrote the following code and used Xpath and CSS selector to give the address, and the Click method, but it does not work properly. Has anyone solved this problem?
import time
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
longInfo = ["xxx.gmail.com", "11"]
try:
driver.get("https://www.tradingview.com/#signin")
driver.set_page_load_timeout(20)
driver.maximize_window()
# click email Button for logging page
driver.find_element_by_xpath("/html/body/div[7]/div/div[2]/div/div/div/div/div/div/div[1]/div[4]/div/span").click()
time.sleep(5)
driver.find_element_by_css_selector(
"#email-signin__user-name-input__e07a4b49-2f94-4b3e-a3f8-934a5744fe02").send_keys(longInfo[0])
driver.find_element_by_css_selector(
"#email-signin__password-input__e07a4b49-2f94-4b3e-a3f8-934a5744fe02").send_keys(longInfo[1])
# click sign in Button
driver.find_element_by_xpath(
"/html/body/div[7]/div/div[2]/div/div/div/div/div/div/form/div[5]/div[2]/button/span[2]").click()
input("type for exit")
driver.quit()
except Exception as e:
print(e)
driver.quit()
It seems the locator you have used its dynamic, you need to identify the element with better approach.
Its required synchronization time while navigating.
Use explicit wait and wait for element to be clickable.
longInfo = ["xxx.gmail.com", "11"]
driver.get("https://www.tradingview.com/#signin")
wait=WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Email']"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[name='username']"))).send_keys(longInfo[0])
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[name='password']"))).send_keys(longInfo[1])
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[.//span[contains(., 'Sign in')]]"))).click()
Use following libraries.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
These values of the id attribute:
email-signin__user-name-input__e07a4b49-2f94-4b3e-a3f8-934a5744fe02
email-signin__password-input__e07a4b49-2f94-4b3e-a3f8-934a5744fe02
are dynamically generated and would change everytime you access the webpage afresh. Instead you need to use locator strategies based on static attributes.
Solution
To login within Trading View website you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategies:
driver.get("https://www.tradingview.com/#signin")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Email']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='username']"))).send_keys("matin_mhz#stackoverflow.com")
driver.find_element(By.XPATH, "//input[#name='password']").send_keys("matin_mhz" + Keys.RETURN)
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
I tried to download the Youtube charts Weekly from https://charts.youtube.com/charts/TopSongs/ as csv. (the download button is in the upper right corner in a SVG icon)
I used this code and tried two ways to click it but both gave me this error selenium.common.exceptions.JavascriptException: Message: javascript error: rootNode.elementsFromPoint is not a function (Session info: chrome=91.0.4472.106)"
And this is my code, I already make sure that I found the right HTML element with download_button.get_attribute("outerHTML")
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://charts.youtube.com/charts/TopSongs/')
time.sleep(4)
#######first attempt######
download_button = driver.find_element_by_xpath("//div[#class='download-container style-scope ytmc-charts']/paper-icon-button")
action = ActionChains(driver)
action.move_to_element(download_button)
action.click()
action.perform()
#######second attempt######
wait = WebDriverWait(driver, 10)
check_box_el = wait.until(ec.visibility_of_element_located((By.XPATH, "//div[#class='download-container style-scope ytmc-charts']/paper-icon-button")))
ActionChains(driver).move_to_element(check_box_el).click().perform()
driver.quit()
Any idea about it? Thanks :)
See if this works:-
download_elm = driver.find_element_by_xpath(".//*[#id='download-button']")
driver.execute_script("arguments[0].click();", download_elm)
I am currently trying to learn selenium in Python and I am having an issue clicking the "Accept All Cookies" button.
I am using:
Python v3.9
Chrome v87
This is the HTML page i am trying to scrape
https://www.currys.co.uk/gbuk/tv-and-home-entertainment/televisions/televisions/samsung-ue75tu7020kxxu-75-smart-4k-ultra-hd-hdr-led-tv-10213562-pdt.html
Here is my code currently
# Selenium Tutorial #1
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Chrome(r"C:\Users\Ste1337\Desktop\chromedriver\chromedriver.exe")
driver.get("https://www.currys.co.uk/gbuk/tv-and-home-entertainment/televisions/televisions/samsung-ue75tu7020kxxu-75-smart-4k-ultra-hd-hdr-led-tv-10213562-pdt.html")
#search = driver.find_element_by_id(ContentPlaceHolder1_NotifyBtn)
driver.implicitly_wait(10)
link = driver.find_element_by_id("onetrust-accept-btn-handler")
link.click
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "onetrust-accept-btn-handler"))
)
element.click
except:
driver.quit()
The "( )" is missing after the click.
Try this:
link = driver.find_element_by_id("onetrust-accept-btn-handler")
link.click()
Simply wait and click.
wait = WebDriverWait(driver, 10)
driver.get("https://www.currys.co.uk/gbuk/tv-and-home-entertainment/televisions/televisions/samsung-ue75tu7020kxxu-75-smart-4k-ultra-hd-hdr-led-tv-10213562-pdt.html")
wait.until(EC.element_to_be_clickable((By.ID, "onetrust-accept-btn-handler"))).click()
I'm trying to create an automation test in Asos (for practice purpose only) however I'm having a hard time locating this sign-in element...
I need to click on that sign-in button.
these are the element I got in inspect:
a class="_1336dMe _1uUU2Co _1336dMe _1uUU2Co" href="https://my.asos.com/my-account?
lang=en-GB&store=COM&country=GB&keyStoreDataversion=3pmn72e-27"
data-testid="signin-link" tabindex="-1">Sign In
I had the same problem when trying to find this button on Google Maps. Is the sign in button on a pop up window? Then the problem is becuse you have to change between frames.
Here is a code sample:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome('driver path')
url = 'url'
driver.get(url)
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//*[#id="consent-bump"]/div/div[1]/iframe')))
agree = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="introAgreeButton"]/span/span')))
agree.click()
#back to the main page
driver.switch_to_default_content()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="searchboxinput"]'))).send_keys('gostilne')
search = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="searchbox-searchbutton"]')))
search.click()
Make shure that the xpath for frames and buttons is correct.
I am trying to click the google store once the google webpage loads. I do not want to use time.sleep() for a few seconds for the google page to load in. I want the browser to click "store" once the page loads. Below is my code, what am I doing wrong?
from selenium import webdriver
import requests
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import pause
driver = webdriver.Chrome('/applications/chromedriver')
driver.set_window_size(1024, 600)
driver.maximize_window()
driver.get("https://www.google.com")
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.xpath, "/html/body/div[2]/div[2]/div[1]/a[2]")))
element.click()
The html for xpath is correct too since it works with driver.find_element_by_xpath("/html/body/div[2]/div[2]/div[1]/a[2]").click()