Problem with click audio challenge recaptcha v2 by using selenium - python

I am working on website that contain two recaptcha v2, First one solved perfect but the next one open captcha frame but cant click the audio button. I have tried more than method to click the button of audio challenge but i cant get any solution.
.html:
<iframe title="reCAPTCHA" src="https://www.google.com/recaptcha/api2/anchor?ar=1&k=6LcMzpsUAAAAAD2JOVSS9DYyk7aSbk-KQ3KL7Nkv&co=aHR0cHM6Ly9tdXFlZW0uc2E6NDQz&hl=ar-ly&type=image&v=Q_rrUPkK1sXoHi4wbuDTgcQR&theme=light&size=normal&cb=flmxkon1yi8a" width="304" height="78" role="presentation" name="a-fzpsv3rm6omy" frameborder="0" scrolling="no" sandbox="allow-forms allow-popups allow-same-origin allow-scripts allow-top-navigation allow-modals allow-popups-to-escape-sandbox" cd_frame_id_="6857a4b8282493a9397ca67d60e7e461"></iframe>
.py:
frames = driver.find_elements_by_tag_name("iframe")
driver.switch_to.frame(frames[0])
delay()
# click on checkbox to activate recaptcha
driver.find_element_by_xpath('/html/body/div[2]/div[3]/div[1]/div/div/span/div[1]').click()
# switch to recaptcha audio control frame
html = driver.find_element_by_tag_name('html')
html.send_keys(Keys.PAGE_DOWN)
driver.switch_to.default_content()
frames = driver.find_element_by_xpath("/html/body/div[2]/div[4]").find_elements_by_tag_name("iframe")
driver.switch_to.frame(frames[1])
delay()
# click on audio challenge
driver.find_element_by_id("recaptcha-audio-button").click()
I tried to change |driver.switch_to.frame(frames1)| from 0 to 7 but still cant click the button and show me this error:
driver.switch_to.frame(frames[1])
IndexError: list index out of range
Any kind of help please?

See you are using tagname, for iframe. so why don't you directly try, find_elements with tag_name ?
instead of this :
frames = driver.find_element_by_xpath("/html/body/div[2]/div[4]").find_elements_by_tag_name("iframe")
do this :
all_iframes = driver.find_elements_by_tag_name("iframe")
and then print the size of this list,
print(len(all_iframes ))
and see if it has something then iterate otherwise you will end up getting IndexError: list index out of range
The below loop is just for testing :-
for iframe in all_iframes:
print(iframe.text)

Related

How to click a button using selenium and draw on a canvas in python?

so I am trying to automate a program that would log in to a google account I created, go to this canvas website, and draw a circle,
(Circle just as a placeholder because im trying to make it draw some cool stuff like a car, just to test if it will work first.) But the main issue is when you first go into the website, there is a pop up that displays and that pop up has 2 options, "learn more" and "Get started". I tried to make selenium click on the "Get started" using driver.find_element_by_id('Get-started').click but it does not seem to work, I also tried to use CSS selector but that does not seem to work either. So, I'm stuck on this. Any advice or help to click the get started button? (Feel free to also give me advice on how to draw in the canvas as well!)
Here's the HTML:
<paper-button id="get-started" dialog-confirm="" class="primary" aria-label="Get started" role="button" tabindex="0" animated="" elevation="0" aria-disabled="false">
Get started
</paper-button>
here's the code:
from selenium import webdriver
import time
from PrivateInfo import*
driver = webdriver.Chrome(r"D:\chromeDriver\chromedriver.exe")
link = "https://www.google.com/"
driver.get(link)
def linkText(element):
button = driver.find_element_by_link_text(element)
button.click()
def byClass(className):
button2 = driver.find_element_by_class_name(className)
button2.click()
def type(text, elements):
input = driver.find_elements_by_name(elements)
for element in input:
driver.implicitly_wait(2)
element.send_keys(text)
linkText("Sign in")
type(email, "identifier")
byClass("VfPpkd-vQzf8d")
type(pw, "password")
driver.find_element_by_id("passwordNext").click()
time.sleep(1)
driver.get("https://canvas.apps.chrome/")
driver.implicitly_wait(3)
driver.find_element_by_css_selector('paper-button[id="get-started"]').click()
edit: I also tried this
getStart = driver.find_elements_by_css_selector('paper-button[id="get-started"]')
for start in getStart:
start.click()
it doesn't give me any errors but it does not do anything.
Ah yeah, forgot to mention but im new to using selenium.
The content of the popup is nested inside shadowRoots.
More on ShadowRoot here:
The ShadowRoot interface of the Shadow DOM API is the root node of a
DOM subtree that is rendered separately from a document's main DOM
tree.
To be able to control the element you will need to switch between DOMs. Here's how:
drawing_app_el = driver.execute_script("return arguments[0].shadowRoot", driver.find_element(By.CSS_SELECTOR, 'drawing-app'))
This code will retrieve the drawing_app first and then return the content of the shadowRoot.
To have access to the button getStarted, here's how:
# Get the shadowRoot of drawing-app
drawing_app_el = driver.execute_script("return arguments[0].shadowRoot", driver.find_element(By.CSS_SELECTOR, 'drawing-app'))
# Get the shadowRoot of welcome-dialog within drawing_app
welcome_dialog_el = driver.execute_script("return arguments[0].shadowRoot", drawing_app_el.find_element(By.CSS_SELECTOR, 'welcome-dialog'))
# Get the paper-dialog from welcome_dialog
paper_dialog_el = welcome_dialog_el.find_element(By.CSS_SELECTOR, 'paper-dialog')
# Finally, retrieve the getStarted button
get_started_button = paper_dialog_el.find_element(By.CSS_SELECTOR, '#get-started')
get_started_button.click()

Is there a way to block/close iframes using seleium?

I am creating a downloader from Instagram. This program gets URLs from top Instagram posts in a given hashtag and inputs them into a downloader. The issue is that a popup iframe ad always appears over the download button when the website is first loaded. This throws an error that the button cannot be clicked because the iframe will be clicked.
This is for Python Selenium running Chrome driver. I have tried to run a filter that finds iframes and goes back to the main page:
all_iframes = self.browser.find_elements_by_tag_name("iframe")
if len(all_iframes) > 0:
self.browser.switch_to.default_content()
This did not work, I also tried to get the XPath to the X-button on the ad, but the ID changes every time, so I cannot be clicked or identified.
#get the website
for link in self.links:
self.browser.get('https://downloadgram.com/')
time.sleep(5)
#this is the x button click iframe I tried, but the XPath changes
all_iframes = self.browser.find_elements_by_tag_name("iframe")
if len(all_iframes) > 0:
xButton = self.browser.find_element_by_xpath('//div[#id="id3019a64023cross3019a64023"]')
xButton.click()
#inputs the URL from array links[] into download box
input = self.browser.find_element_by_xpath('//input[#name="url"]')
input.clear()
input.send_keys(link)
time.sleep(1)
#clicks download button
download = self.browser.find_element_by_xpath("//input[#type='submit']")
download.click()
time.sleep(1)
#clicks confirm button
actuallyDownload = self.browser.find_element_by_xpath("//a[#target='_blank']")
actuallyDownload.click()
time.sleep(1)
I expect the code to download the pictures at the url, but I get:
selenium.common.exceptions.WebDriverException: Message: unknown error: Element <input type="submit" value="Download" class="button"> is not clickable at point (451, 446). Other element would receive the click:
the website (turn off adblock to see add) https://downloadgram.com/
I am able to close the pop up with following code.Please try that.
browser.get('https://downloadgram.com/')
time.sleep(5)
element=browser.find_element_by_xpath("//div[starts-with(#id,'id')]" and "//div[starts-with(#style,'position:absolute !important;height:20px !important;width:20px !important;top:3px !important;left:3px !important;background-image:url(data:image/png;')]")
arrt=element.get_attribute("id")
print(arrt)
browser.execute_script("arguments[0].click();", element)
Let me know if it works.Good Luck.

Empty 'src' attribute returned with Selenium on Python

I am a rookie programmer and I am teaching myself some webscraping. I am trying to make a Python program that returns the direct video download URL from an embedded player by scraping a webpage with selenium.
So here's the relevant html for the webpage:
<video class="vjs_tech" id="olvideo_html5_api" crossorigin="anonymous"></video>
<button class="vjs-big-play-button" type="button" aria-live="polite" title="Play Video" aria-disabled="false"><span class="vjs-control-text">Play Video</span></button>
The video element initially does not have a src attribute. But when I click the above button on my browser, the page seems to run a few javascripts and the video element gets an src attribute. I want to print the contents of this src attribute to the monitor. So this is how I replicated this process in python:
#Clicking the Button
playbutton = driver.find_element_by_tag_name('button')
playbutton.send_keys(Keys.RETURN)
#Selecting the Video Element
wait = WebDriverWait(driver, 5)
video = wait.until(EC.visibility_of_element_located((By.TAG_NAME, 'video')))
#Printing the details of the Video Element
print "Class: ", video.get_attribute("class")
print "ID: ", video.get_attribute("id")
print "SRC: ", video.get_attribute("src")
The output looks like this:
Class: vjs_tech
ID: olvideo_html5_api
SRC:
As you can see, I can get the 'class' and 'id' info accurately but the 'src' tag always returns empty. But if I use Chrome to open the site and click the button manually, I can see that the src field gets populated as expected.
What am I doing wrong? How can I get the src attribute to show up in my output?
(Im using Selenium with ChromeDriver on Python27.)
I guess it takes some time(could be ms) after you click on 'button' and src to appear in video element. Since video element is always present webdriver will get its current state (i.e. no src ). Implicit/explicit wait will not help here, in this case you will need to use time.sleep
import time
#Clicking the Button
playbutton = driver.find_element_by_tag_name('button')
playbutton.send_keys(Keys.RETURN)
time.sleep(5) #<<<<<<<<<<<<<<<to add 5 sec sleep, you can adjust this
#Selecting the Video Element
video = driver.find_element_by_tag_name('video')
#Printing the details of the Video Element
print "Class: ", video.get_attribute("class")
print "ID: ", video.get_attribute("id")
print "SRC: ", video.get_attribute("src")

Element is not clickable (the button is blocking by other element)

I'm trying to click on this button: browser.find_element_by_id('btnSearch')
But this button is blocking by this div tag: <div id="actionSearch" class="row pull-right">
How do I go around to click this button with id='btnSearch" while it's blocking by the actionSearch div?
I tried the following:
browser.find_element_by_id('btnSearch').click()
browser.implicitly_wait(10)
el = browser.find_element_by_xpath('//*[#id="btnSearch"]')
ActionChains(browser).move_to_element_with_offset(el, 1827, 270)
ActionChains(browser).click()
ActionChains(browser).perform()
element = browser.find_element_by_id('btnSearch')
browser.execute_script("arguments[0].click();", element)
wait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, //*[#id="btnSearch"]'))).click()
none of these work.
Can anyone help me with this? I've spent two days trying to click this button!! Please help!
Considering provided image of HTML source (tip: do not provide it as image, but as text instead) I can assume that required element located in the bottom of page and you might need to scroll page down to be able to handle it.
Try below code
link = browser.find_element_by_id("btnSearch")
browser.execute_script("arguments[0].scrollIntoView()", link)
link.click()
Note that link is not a pseudo-element (is not located inside ::before/::after pseudo-element), so it can not be the cause of your problem
As for your code:
ActionChains(browser).move_to_element_with_offset(el, 1827, 270)
ActionChains(browser).click()
ActionChains(browser).perform()
Here you're trying to make scroll to link with huge offset and then you make click on current mouse position - not on link
You can try to modify it as
ActionChains(browser).move_to_element(el)
ActionChains(browser).click(el) # Pass WebElement you want to click as argument to `click()`
ActionChains(browser).perform()

Python+Selenium, can't click the 'button' wrapped by span

I am new to selenium here. I am trying to use selenium to click a 'more' button to expand the review section everytime after refreshing the page.
The website is TripAdvisor. The logic of more button is, as long as you click on the first more button, it will automatically expand all the review sections for you. In other words, you just need to click on the first 'more' button.
All buttons have a similar class name. An example is like taLnk.hvrIE6.tr415411081.moreLink.ulBlueLinks. Only the numbers part changes everytime.
The full element look like this:
<span class="taLnk hvrIE6 tr413756996 moreLink ulBlueLinks" onclick=" var options = {
flow: 'CORE_COMBINED',
pid: 39415,
onSuccess: function() { ta.util.cookie.setPIDCookie(2247); ta.call('ta.servlet.Reviews.expandReviews', {type: 'dummy'}, ta.id('review_413756996'), 'review_413756996', '1', 2247);; window.location.hash = 'review_413756996'; }
};
ta.call('ta.registration.RegOverlay.show', {type: 'dummy'}, ta.id('review_413756996'), options);
return false;
">
More </span>
I have tried several ways to get the button click. But since it is an onclick event wrapped by span, I can't successfully get it clicked.
My last version looks like this:
driver = webdriver.Firefox()
driver.get(newurl)
page_source = driver.page_source
soup = BeautifulSoup(page_source)
moreID = soup.find("span", class_=re.compile(r'.*\bmoreLink\b.*'))['class']
moreID = '.'.join(moreID[0:(len(moreID)+1)])
moreButton = 'span.' + moreID
button = driver.find_element_by_css_selector(moreButton)
button.click()
time.sleep(10)
However, I keep getting the error message like this:
WebDriverException: Message: Element is not clickable at point (318.5,
7.100006103515625). Other element would receive the click....
Can you advise me on how to fix the problem? Any help will be appreciated!
WebDriverException: Message: Element is not clickable at point (318.5, 7.100006103515625). Other element would receive the click....
This error to be occur when element is not in the view port and selenium couldn't click due to some other overlay element on it. In this case you should try one of these following solution :-
You can try using ActionChains to reach that element before click as below :-
from selenium.webdriver.common.action_chains import ActionChains
button = driver.find_element_by_css_selector(moreButton)
ActionChains(button).move_to_element(element).click().perform()
You can try using execute_script() to reach that element before click as :-
driver.execute_script("arguments[0].scrollIntoView(true)", button)
button.click()
You can try using JavaScript::click() with execute_script() but this JavaScript::click() defeats the purpose of the test. First because it doesn't generate all the events like a real click (focus, blur, mousedown, mouseup...) and second because it doesn't guarantee that a real user can interact with the element. But to get rid from this issues you can consider it as an alternate solution.
driver.execute_script("arguments[0].click()", button)
Note:- Before using these options make sure you're trying to interact with correct element using with correct locator, otherwise WebElement.click() would work well after wait until element visible and clickable using WebDriverWait.
Try using an ActionChains:
from selenium.webdriver.common.action_chains import ActionChains
# Your existing code here
# Minus the `button.click()` line
ActionChains(driver).move_to_element(button).cli‌​ck().perform()
I have used this technique when I need to click on a <div> or a <span> element, rather than an actual button or link.

Categories

Resources