Timeout Waiting for Visible iframe - python-3.x

I'm learning Selenium with Python, and attempting to write happy path flow for a website. Where I get stuck is a part of the flow where an iframe is automatically launched after clicking through a pop-up window. I've tried several different methods, but am unable to locate the frame, or wait for it appear. Either it's not found or times out.
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.ID, 'continue- reservation'))).click()
# cvv2 form
WebDriverWait(driver, 5).until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'cvv_iframe')))
Error:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'cvv_iframe')))
File "/Users/anutter/venv/lib/python3.7/site-packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
HTML is as follows:
<div id="cpstaging" class="emptystage">
<div class="fixed h-v-centered new-preload" style="display: none;"><span class="fa fa-spinner f-50 f-grn fa-pulse"></span></div>
</div>
<div id="cvv-box" class="pad-30-lr">
<div id="enter-cvv-title" class="blk mar-20-b f-18 border-b pad-10-b">Enter CVV Code</div>
<iframe src="https://qa-hotels.ecbsn.com/cvv?oauth_token=u7q99%2Fe8I%2BkUrkLMr4dGR2t4gmcDbVtr&type=visa&src=web-desktop" id="cvv_iframe" name="cvv_iframe" width="430" height="160" frameborder="0" seamless="seamless" scrolling="no" sandbox="allow-same-origin allow-scripts allow-popups allow-forms"></iframe>
</div>
</div>
</div>
</div>

As per the HTML you have shared to access the elements within the <iframe> you have to:
Induce WebDriverWait for the desired frame to be available and switch to it using either of the Locator Strategies:
Using ID:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"cvv_iframe")))
Using NAME:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"cvv_iframe")))
Using CSS_SELECTOR:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#cvv_iframe[name='cvv_iframe']")))
Using XPATH:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='cvv_iframe' and #name='cvv_iframe']")))
Note: As per your code trials the ID of the <iframe> isn't cvv-iframe but cvv_iframe
Here you can find a relevant discussion on Ways to deal with #document under iframe

Related

How can I click on elements using Selenium Python?

I'm trying to generalize much as possible a code in order to scrape eBay website.
I am now stuck in this situation:
this is the following URL where I start:
https://www.ebay.co.uk/sch/i.html?_from=R40&_nkw=iphone+12&_oac=1
I want to click on the "More filters" button and then select specifically the 'Brand' option and get the list of the available brands.
This is what I was able to do till now:
url = 'https://www.ebay.co.uk/sch/i.html?_from=R40&_nkw=iphone+12&_oac=1'
driver = webdriver.Chrome()
driver.get(url)
driver.find_element_by_id('s0-14-11-0-1-2-6-2').click()
after click() the sub panel is open and I get what I need to change:
<div role="tab" class="x-overlay-aspect " data-aspecttitle="aspect-Brand" aria-selected="false" aria-controls="refineOverlay-subPanel" id="c3-mainPanel-Brand"><span class="x-overlay-aspect__label">Brand</span><svg focusable="false" aria-hidden="true" class="x-overlay-aspect__check-icon svg-icon icon-check" role="img" aria-label="Filter applied"><use xlink:href="#svg-icon-check"></use></svg></div>
if I select manually (i.e. clicking with the cursor) on 'Brand' here is what I get back:
<div role="tab" class="x-overlay-aspect active" tabindex="0" data-aspecttitle="aspect-Brand" aria-selected="true" aria-controls="refineOverlay-subPanel" id="c3-mainPanel-Brand"><span class="x-overlay-aspect__label">Brand</span><svg focusable="false" aria-hidden="true" class="x-overlay-aspect__check-icon svg-icon icon-check" role="img" aria-label="Filter applied"><use xlink:href="#svg-icon-check"></use></svg></div>
Unfortunately, I barely know Javascript so, althought there are other similar posts, I don't know how to keep going from here in order to get finally the information I need.
I hope you can help and I thank you in advance!
To click More filters and then to click on Brand you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Code Block:
driver.get("https://www.ebay.co.uk/sch/i.html?_from=R40&_nkw=iphone+12&_oac=1")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'More filters')]"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Brand']"))).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:

CSS selector for reCAPTCHA checkbok using Selenium and VBA Excel

On the website I am trying to fill in some fields on, there is a checkbox that I need to click to add the check mark in it:
<div class="rc-anchor-content"><div class="rc-inline-block"><div class="rc-anchor-center-container"><div class="rc-anchor-center-item rc-anchor-checkbox-holder"><span class="recaptcha-checkbox goog-inline-block recaptcha-checkbox-unchecked rc-anchor-checkbox recaptcha-checkbox-expired" role="checkbox" aria-checked="false" id="recaptcha-anchor" dir="ltr" aria-labelledby="recaptcha-anchor-label" aria-disabled="false" tabindex="0"><div class="recaptcha-checkbox-border" role="presentation" style=""></div><div class="recaptcha-checkbox-borderAnimation" role="presentation" style=""></div><div class="recaptcha-checkbox-spinner" role="presentation" style="transform: rotate(180deg);"></div><div class="recaptcha-checkbox-spinnerAnimation" role="presentation" style=""></div><div class="recaptcha-checkbox-checkmark" role="presentation"></div></span></div></div></div><div class="rc-inline-block"><div class="rc-anchor-center-container"><label class="rc-anchor-center-item rc-anchor-checkbox-label" aria-hidden="true" role="presentation" id="recaptcha-anchor-label"><span aria-live="polite" aria-labelledby="recaptcha-accessible-status"></span>I'm not a robot</label></div></div></div>
Using Selenium in VBA, I tried the following
.FindElementByCss("div.recaptcha-checkbox-border").Click
And also I tried
.FindElementByCss("span.recaptcha-checkbox").Click
But I got an error at this line.
Here's the link of the website to see the whole HTML
https://www.moj.gov.kw/AR/E-Gov/Pages/eServices01.aspx
To click() on the element, as the desired element is within an <iframe> so you have to:
Induce a waiter and switch to the desired frame.
Induce a waiter for the desired element to be clickable.
You can use the following solution:
.SwitchToFrame.FindElementByXPath("//iframe[contains(#src, 'recaptcha') and not(#title='recaptcha challenge')]", timeout:=10000)
.FindElementByCss("div.recaptcha-checkbox-checkmark").Click
You can find similar discussions in:
How to click on the reCaptcha using Selenium and Java
Find the reCAPTCHA element and click on it — Python + Selenium
Here you can find a relevant discussion on Ways to deal with #document under iframe

Need help to select radio button in Chrome (Part II) using Excel VBA Selenium Basic

I'm struggling once more trying to select a radio button in Chrome (v 75.0.3770.100) using Selenium Basic ChromeDriver (v 75.0.3770.140) in Excel (2013) VBE. Here's the HTML:
<div class=radio-wrap>
<input id="shipToSelect" type="radio" class="radio"
name="soldToShipToselection" value="SH" ng-click="radioClicked($event)">
<label for="shipToSelect" class="radio-label">
<i></i>
<span>Ship-to</span>
</label>
</div>
I've tried unsuccessfully with the below:
obj.FindElementByCss("div.radio-wrap>input.radio[id$='radio']").Click
obj.FindElementByName("soldToShipToselection").Click
obj.FindElementById("shipToSelect").Click
obj.FindElementByXPath("//div[#class='radio-wrap']/input[.,(#id,'shipToSelect')]").Click
To click on the radio button associated with the text as Ship-to you can use either of the following Locator Strategies:
cssSelector:
driver.FindElementByCss("label[for='shipToSelect']").Click
xpath:
driver.FindElementByXPath("//label[#for='shipToSelect']").Click
Note: As it is a JavaScript enabled element you need to induce a waiter for the element to be clickable
Update
As an alternative, you can try either of the folloing Locator Strategies:
cssSelector:
driver.FindElementByCss("input.radio#shipToSelect[name='soldToShipToselection']").Click
xpath:
driver.FindElementByXPath("//input[#class='radio' and #id='shipToSelect'][#name='soldToShipToselection']").Click
Try the following Xpath to click on radio button.Use Explicit wait before interacting the element.
obj.FindElementByXPath("//div[#class='radio-wrap']/input[#id='shipToSelect']").Click

How do i refer to this element with Selenium (python)?

So i'm trying to automate this simple thing, where i want to click on a image.
Here is HTML:
<div class="guild-inner" draggable="false" style="border-radius: 15px;">
<a draggable="false" class="avatar-small" href="/channels/223259649967652887/381675339702599690" style="background-image: url("https://cdn.discordapp.com/icons/223259649967652887/2be8d9ec8759056fabe8caa306c632c8.png");">
</a>
</div>
Here is my code:
def find_tc():
driver.find_element_by_xpath('//a[#href="/channels/223259649967652887/381675339702599690"]').click()
wait(5)
Here is the error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//a[href="/channels/223259649967652887/381675339702599690"]"}
I have to refer this by href because that is the only thing that is unique. All the images are dynamically defined so i also can't refer to the image by on which number it is.Thanks in advance! (new to selenium)
Ok, so the problem is that i was trying to find the element when the page is not completely loaded. So i had to put like one minute sleep time before initiating the search for the element..because the java script is still being loaded in the background. Still is there any was to decide if the page has been completely loaded including java script instead of putting a random sleep time? happy to learn!

Selenium Webdriver python XPath not working

I am trying to automate a process using Python and selenium webdriver.
I am able to login successfully and navigating to the page where I want to post something but for some reason the xpath is not recognized by the system.
It gives the below error:
NoSuchElementException: Unable to locate element: //*[#id="widecol"]/div/form/p[1]/input
Here is my code:
from selenium import webdriver
mydriver = webdriver.Firefox()
mydriver.get("https://www.phishtank.com/")
mydriver.maximize_window()
mydriver.find_element_by_xpath('//*[#id="username"]').send_keys("myusername")
mydriver.find_element_by_xpath('//*[#id="password"]').send_keys("mypassword")
mydriver.find_element_by_xpath('//*[#id="header"]/div[2]/form/input[3]').click()
mydriver.find_element_by_xpath('//*[#id="nav"]/ul/li[2]/a').click()
mydriver.find_element_by_xpath('//*[#id="widecol"]/div/form/p[1]/input').send_keys("sample text testing to fill form")
This is my HTML code
<div id="widecol">
<div class="padded">
<form method="POST">
<h2>Add A Phish</h2>
<ol>
<li>Visit our <b>What is phishing?</b> page to confirm that the suspected phish meets all of the criteria.</li>
<li>Add a phish using the form below, or even better, submit a phish directly via email.</li>
</ol>
<h3>Phish URL:</h3>
<p>
<input type="text" name="phish_url" style="width:90%;" value="" /><br />
<span class="small">Copy and paste the URL of the phishing website.</span>
</p>
<h3>What is the organization referenced in the email?</h3>
<p class="slim">
<select name="phish_target">
which gives me the following error:
NoSuchElementException: Unable to locate element: //*[#id="widecol"]/div/form/p[1]/input
Here is the HTML code:
<input type="text" name="phish_url" style="width:90%;" value=""> outer HTML code
And this is my XPath:
//*[#id="widecol"]/div/form/p[1]/input - Xpath
Please let me know where to look, thank you.
You need to induce WebDriverWait for the element to be clickable and you can use the following line of code :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='phish_url' and #type='text']"))).send_keys("sample text testing to fill form")
Without the full HTML, we can't tell if the XPath is correct or no.
Can you please try and include more outer Html code?
You can try and wait after clicking on that last URL, maybe the element wasn't loaded yet.
You can also check if the input is in an iframe, that might be causing the problem.
Could you try the following instead?
//*[#id='widecol']//input[#name='phish_url']
Should work for that input. Just for trial/error sake, try a small wait before doing this.
The new Selenium version doesn't allow it.
Try the below code
from selenium.webdriver.common.by import By
And then Change the 'find_element_by_xpath statements' to
mydriver.find_element(By.XPATH,'//[#id="username"]').send_keys("myusername")
You can refer to the link to know more

Resources