Selenium - How to debug timeout for iframe > input field? - python

I'm stuck writing a Selenium WebDriver script for Instagram web login. I think I switched to the appropriate iframe but WebDriver keeps timing out when it should locate the user input field.
Relevant source from Instagram site:
https://instagram.com/accounts/login/
<iframe class="hiFrame" data-reactid=".0.0.0.1.0.1.0.0.$frame" src="https://instagram.com/accounts/login/ajax/?targetOrigin=https%3A%2F%2Finstagram.com" scrolling="no" seamless="">
<!DOCTYPE html>
<html class="hl-en not-logged-in " lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<body class="LoginFormChrome ">
<div class="LoginFormPage" data-reactid=".0">
<form data-reactid=".0.0">
<p class="lfField" data-reactid=".0.0.0">
<label class="lfFieldLabel" data-reactid=".0.0.0.0">
<input class="lfFieldInput" type="text" data-reactid=".0.0.0.1" value="" autocorrect="false" autocapitalize="false" maxlength="30" name="username">
</p>
Source from Selenium script:
login_url = 'https://instagram.com/accounts/login/'
profile_url = '<path_firefix_profile>'
user = '<user_name>'
#login
my_profile = FirefoxProfile(profile_url)
self.driver = webdriver.Firefox(my_profile)
self.driver.get(login_url)
self.driver.implicitly_wait(10)
my_iframe = self.driver.find_element_by_css_selector("iframe.hiFrame")
#my_iframe = self.driver.find_element_by_css_selector("iframe:nth-of-type(1)")
#my_iframe = self.driver.find_element_by_tag_name("iframe")
self.driver.switch_to_frame(my_iframe)
try:
element = WebDriverWait(self.driver, 30).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[name='username']")))
user_input = self.driver.find_element_by_css_selector("input[name='username']")
user_input.send_keys(user)
finally:
print('user name input appeared')
Results:
This error results from WebDriver:
File "instagram_firefox.py", line 51, in setUp
element = WebDriverWait(self.driver, 45).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[name='username']")))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/support/wait.py", line 71, in until
raise TimeoutException(message)
I tried to verify that the css selector for the input field was correct. On the page, https://instagram.com/accounts/login/, FireFox FireFinder does not recognize the css selector that I used. But if I open another tab with the source of the iframe, https://instagram.com/accounts/login/ajax/?targetOrigin=https%3A%2F%2Finstagram.com, then Firefinder recognizes the css selector that I used. Does this mean I need to manually get the url of the iframe source or should that be done automatically when WebDriver switches to the iframe?

We should wait first for div spinner element to disappear, then we can retrieve that iframe you need:
user = "user"
self.driver.get("https://instagram.com/accounts/login/")
#Wait for spinner to disappear
WebDriverWait(self.driver, 10).until(EC.invisibility_of_element_located((By.CSS_SELECTOR, "div.liSpinnerLayer")))
#Get iframe and switch to it
my_iframe = self.driver.find_element_by_css_selector("iframe.hiFrame")
self.driver.switch_to_frame(my_iframe)
element = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[name='username']")))
element.send_keys(user)

Related

Selenium Webdriver is unable to find any element in a webpage

Trying to write a webscraper but I need to get into a website that requires an account. I'm going through 2 factor authentication to get in as that felt more secure than by username and password but I can't get Selenium to find the input for the 2fa code.
I tried every type of locater available and none of those work. The website does not appear to have a frame so I don't think that's the issue. I'm using an implicit wait as well so I believe the webpage has enough time to load.
I'm not really sure what I'm doing wrong at this point. I just keep getting an Unable to locate element error
Here the html for the item
<input class="sc-jzJRlG hxEkIM sc-VigVT gupvKM" data-testid="security-code" id="security-code" value="" autocomplete="off" autocorrect="off" spellcheck="false" maxlength="255" type="password" data-reactid=".0.1.0.2.1.1.0.0.1.0">
and my code
driver.implicitly_wait(10)
codeInput = driver.find_element(By.XPATH, '/html/body/div/div/div[2]/div[1]/div[3]/div/form/div/div[1]/div[2]/input')
enterButton = driver.find_element(By.XPATH, '/html/body/div/div/div[2]/div[1]/div[3]/div/form/div/div[2]/div/button')
codeInput.send_keys('123456')
action.click(enterButton)
action.perform()
and the Stacktrace
Message=Message: Unable to locate element: /html/body/div/div/div[2]/div[1]/div[3]/div/form/div/div[1]/div[2]/input
Stacktrace:
RemoteError#chrome://remote/content/shared/RemoteError.jsm:12:1
WebDriverError#chrome://remote/content/shared/webdriver/Errors.jsm:192:5
NoSuchElementError#chrome://remote/content/shared/webdriver/Errors.jsm:404:5
element.find/</<#chrome://remote/content/marionette/element.js:291:16
Source=C:\Users\user\source\repos\WebScraper\WebScraper\webscraper.py
StackTrace:
File "C:\Users\user\source\repos\WebScraper\WebScraper\webscraper.py", line 27, in <module> (Current frame)
codeInput = driver.find_element(By.XPATH, '/html/body/div/div/div[2]/div[1]/div[3]/div/form/div/div[1]/div[2]/input')

Getting error while uploading the file using selenium

This is the error when i click on OKI am trying to automate process with selenium in python so one of the step is to upload file below is the input element before selenium tried to upload the file:
<input ct="FU" lsdata="{1:'Content',7:'ONSUBMIT',8:true}" lsevents="{Change:[{ResponseData:'delta',EnqueueCardinality:'single'},{}]}" id="WD039B" name="WD039B" type="file" tabindex="0" ti="0" class="lsFileupload__input urBorderBox urUpld" size="0" title="Content">
When selenium upload the file and click on "OK" i am getting error like file has not been chosen but i can see the file name against "Choose File" below is the same input element when selenium tried to click on "OK" after uploading:
<input ct="FU" lsdata="{0:'ERROR',1:'Content',7:'ONSUBMIT',8:true}" lsevents="{Change:[{ResponseData:'delta',EnqueueCardinality:'single'},{}]}" id="WD030D" name="WD030D" type="file" tabindex="0" ti="0" class="lsFileupload__input urBorderBox urUpldInv" size="0" title="Content">
When i manually tried to upload file after uploading same element looks like this:
<input ct="FU" lsdata="{1:'Content',7:'ONSUBMIT',8:true}" lsevents="{Change:[{ResponseData:'delta',EnqueueCardinality:'single'},{}]}" id="WD0302" name="WD0302" type="file" tabindex="0" ti="0" class="lsFileupload__input urBorderBox urUpld" size="0" title="Content" style="background-image: none;">
I am using following selenium code to upload the file:
upload_element=WebDriverWait(driver,v[3]/table/tbody/tr/td/div/div[1]/table/tbody/tr[2]/td/div/div/div/div/table/tbody/tr[1]/td[2]/form/input")))
time.sleep(5)
#driver.execute_script("arguments[0].removeAttribute('style')", upload_element)
time.sleep(5)
#To upload the file
upload_element.send_keys("C:\\Users\\e0481737\\.PyCharmCE2018.2\\config\\scratches\\Help File.pdf")
driver.execute_script("arguments[0].style.display = 'background-image: none;'; return arguments[0];", upload_element)
# until this point I am successful but when I click on "OK" it says that file is not chosen
time.sleep(5)
# to click on "OK" button
element = WebDriverWait(driver, wait_time).until(
EC.element_to_be_clickable((By.XPATH, "/html/body/table/tbody/tr/td/div/div[1]/div/div[4]/div/table/tbody/tr/td[3]/table/tbody/tr/td[1]/div")))
time.sleep(5)
element.click()

Clicking the hidden input Selenium in Python

I am trying to automate the login procedure with Selenium in Firefox with Python.
That's how a login button looks like in HTML:
<td>
<input name="cmd" value="lg" type="hidden">
<input src="ok.png" style="border-style: none;" type="image">
</td>
I have tried a following method:
loginButton = driver.find_elements_by_xpath("//input[#name='cmd' and #value='lg']")[0]
loginButton.click()
It returns the following exception with an empty message.
"selenium.common.exceptions.ElementNotInteractableException: Message: "
This method returns
"Message: Element is not visible"
loginButton = driver.find_element_by_name("cmd")
loginButton.send_keys(Keys.RETURN)
Could you please explain what I am missing?
If you want to click on input next to hidden, try
loginButton = driver.find_element_by_xpath("//input[#src='ok.png']")
# loginButton = driver.find_element_by_xpath("//input[#name='cmd' and #value='lg']/following-sibling::input")
loginButton.click()

Selenium python click button

I just can't make the button click and go the the next page. I tried the following so far
<div class="step__footer">
<button class="step__footer__continue-btn btn " type="submit" name="button">
<span class="btn__content">Continue to payment method</span>
<i class="btn__spinner icon icon--button-spinner"></i>
</button>
<a class="step__footer__previous-link" href="https://checkout.shopify.com/946304/checkouts/1b6e3391268707abb18850300b89e59?step=contact_information">
<svg class="previous-link__icon icon--chevron icon" viewBox="0 0 6.7 11.3" height="11.3" width="6.7" xmlns="http://www.w3.org/2000/svg">
Return to customer information
</a>
</div>
driver = webdriver.Firefox()
driver.implicitly_wait(1) # seconds
driver.find_element_by_css_selector("step__footer__continue-btn btn").click()
driver.implicitly_wait(1) # seconds
driver.find_element_by_xpath("/html/body/div[2]/div/div[2]/div[2]/div/form/div[2]/button").click()
driver.implicitly_wait(1) # seconds
driver.find_element_by_name("button").click()
driver.implicitly_wait(1) # seconds
elem.send_keys(Keys.RETURN)
driver.implicitly_wait(1) # seconds
driver.find_element_by_css_selector("div.step__footer>button").click()
driver.implicitly_wait(1) # seconds
driver.find_element_by_css_selector("html.multi-step.mac.firefox.desktop.page--no-banner.page--logo-main.page--show.js.flexbox.flexboxlegacy.rgba.multiplebgs.boxshadow.opacity.cssanimations.csstransitions.generatedcontent.svg.inlinesvg.cors.boxsizing.display-table.pointerevents.placeholder.mediaqueries.floating-labels body div.content div.wrap div.main div.main__content div.step form.edit_checkout.animate-floating-labels div.step__footer button.step__footer__continue-btn.btn").click()
driver.implicitly_wait(1) # seconds
driver.find_element_by_link_text("Continue to payment method").click()
driver.implicitly_wait(1) # seconds
driver.find_elements_by_class_name("step__footer__continue-btn btn ").click()
driver.implicitly_wait(1) # seconds
driver.findElementByXpath("//div[#type='submit'][#name='button']").click();
EDIT
The key was to relocate the elements, to reload. Since I was clicking through a form, the underlying code changed.
the driver.find_element_by_xpath("//button[#type='submit'][#name='button']").click() could not find elements because of that. After reloading with driver.get ("%s/%s:%s" % (str(sys.argv[4]), str(sys.argv[2]), str(sys.argv[3]))) it worked.
Also look at the log in case some badly formatted command interrupts the flow.
You have many mistakes in your selectors, try one of those
driver.find_element_by_css_selector(".step__footer__continue-btn.btn").click() # for class with css_selector you need '.' in the beginning
driver.find_element_by_class_name("step__footer__continue-btn").click() # by_class_name gets only one class
driver.find_element_by_xpath("//button[#type='submit'][#name='button']").click() # type='submit' and name='button' are <button> tag attributes, not div
You can also try using explicit wait
WebDriverWait(driver, 10).until(expected_conditions.visibility_of_element_located((By.class_name, "step__footer__continue-btn"))).click()
Edit
Sometimes button element area is wider than the element that can receive click. You can try to click on elements in "lower" level
driver.find_element_by_class_name("btn__content").click() # <span> tag
# or
driver.find_element_by_class_name("btn__spinner").click() # <i> tag

How can I get Selenium Web Driver to wait for an element to be accessible, not just present?

I am writing tests for a web application. Some commands pull up dialog boxes that have controls that are visible, but not available for a few moments. (They are greyed out, but webdriver still sees them as visible).
How can I tell Selenium to wait for the element to be actually accessible, and not just visible?
try:
print "about to look for element"
element = WebDriverWait(driver, 10).until(lambda driver : driver.find_element_by_id("createFolderCreateBtn"))
print "still looking?"
finally: print 'yowp'
Here is the code that I have tried, but it "sees" the button before it is usable and basically charges right past the supposed "wait".
Note that I can stuff a ten second sleep into the code instead of this and the code will work properly, but that is ugly, unreliable, and inefficient. But it does prove that the problem is just that "click" command is racing ahead of the availability of the controls.
I assume the events timeline goes like this:
there are no needed elements on page.
needed element appears, but is disabled:
<input type="button" id="createFolderCreateBtn" disabled="disabled" />
needed element becomes enabled:
<input type="button" id="createFolderCreateBtn" />
Currently you are searching for element by id, and you find one on step 2, which is earlier than you need. What you need to do, is to search it by xpath:
//input[#id="createFolderCreateBtn" and not(#disabled)]
Here's the difference:
from lxml import etree
html = """
<input type="button" id="createFolderCreateBtn" disabled="disabled" />
<input type="button" id="createFolderCreateBtn" />
"""
tree = etree.fromstring(html, parser=etree.HTMLParser())
tree.xpath('//input[#id="createFolderCreateBtn"]')
# returns both elements:
# [<Element input at 102a73680>, <Element input at 102a73578>]
tree.xpath('//input[#id="createFolderCreateBtn" and not(#disabled)]')
# returns single element:
# [<Element input at 102a73578>]
To wrap it up, here's your fixed code:
try:
print "about to look for element"
element_xpath = '//input[#id="createFolderCreateBtn" and not(#disabled)]'
element = WebDriverWait(driver, 10).until(
lambda driver : driver.find_element_by_xpath(element_xpath)
)
print "still looking?"
finally:
print 'yowp'
UPDATE:
Repasting the same with the actual webdriver.
Here's the example.html page code:
<input type="button" id="createFolderCreateBtn" disabled="disabled" />
<input type="button" id="createFolderCreateBtn" />
Here's the ipython session:
In [1]: from selenium.webdriver import Firefox
In [2]: browser = Firefox()
In [3]: browser.get('file:///tmp/example.html')
In [4]: browser.find_elements_by_xpath('//input[#id="createFolderCreateBtn"]')
Out[4]:
[<selenium.webdriver.remote.webelement.WebElement at 0x103f75110>,
<selenium.webdriver.remote.webelement.WebElement at 0x103f75150>]
In [5]: browser.find_elements_by_xpath('//input[#id="createFolderCreateBtn" and not(#disabled)]')
Out[5]:
[<selenium.webdriver.remote.webelement.WebElement at 0x103f75290>]
UPDATE 2:
It works with this as well:
<input type="button" id="createFolderCreateBtn" disabled />
print time.time()
try:
print "about to look for element"
def find(driver):
e = driver.find_element_by_id("createFolderCreateBtn")
if (e.get_attribute("disabled")=='true'):
return False
return e
element = WebDriverWait(driver, 10).until(find)
print "still looking?"
finally: print 'yowp'
print "ok, left the loop"
print time.time()
Here is what we ended up with. (Thanks to lukeis and RossPatterson.) Note that we had to find all the items by id and then filter by "disabled". I would have preferred a single search pattern, but what can you do?
I think something along these lines should work as well:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
browser = webdriver.Firefox()
wait = WebDriverWait(browser, 30)
wait.until(expected_conditions.presence_of_element_located((By.XPATH, "//*[#id='createFolderCreateBrn' and not(#disabled)]")))
There are already some great answers posted up here, but I thought I would add my solution. Explicit wait etc. are great functions for use in testing with selenium. However explicit wait merely performs the function of a Thread.Sleep() that you can only set one time. The function below is what I used to "Shave off" a few minutes. It waits until the element is "accessible."
//ALTERNATIVE FOR THREAD.SLEEP
public static class Wait
{
//public static void wait(this IWebDriver driver, List<IWebElement> IWebElementLIst)
public static void wait(this IWebDriver driver, By bylocator)
{
bool elementPresent = IsPresent.isPresent(driver, bylocator);
while (elementPresent != true)
{
Thread.Sleep(1000);
elementPresent = IsPresent.isPresent(driver, bylocator);
}
}
}
It is in C#, but to adapt it would not be that difficult. Hope this helps.

Categories