Selenium unable to locate element - selenium

I have tried all the suggestions on stackoverflow but none seems to work. My code works for all other tested log in site but "https://internet-banking.dbs.com.sg/", which is the site I'm trying to write a script to log in and check my bank balance upon request.
My code is as follow:
def page_is_loaded(driver):
return driver.find_element_by_tag_name("body") != None
driver = webdriver.Firefox()
driver.get("https://internet-banking.dbs.com.sg/")
wait = ui.WebDriverWait(driver, 10)
wait.until(page_is_loaded)
print(driver.page_source)
email_field = driver.find_element_by_id("UID")
email_field.send_keys(*UID*)
password_field = driver.find_element_by_id("PIN")
password_field.send_keys(*PIN*)
password_field.send_keys(Keys.RETURN)
When I run this, I am returned selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element:...
Also, if I check driver.page_source, the result is that of the page.

I'm assuming the line that returns the body tag is the one that is failing. It's because the page hasn't loaded yet so it throws an exception. You should use Expected Conditions, specifically visibility_of_element_located and wait for the UID instead of body.

Related

Rotating Proxies and Selenium - Close driver and re-run script with new IP

I am running an automated script that needs to change IP every 5-6 refreshes. I understand that the it cannot be changed dynamically as it needs to re-open the webdriver with new options. I have a list of proxies in a text file and I wish to use these IPs at random every 5-6 times the driver refreshes. It would normally get stuck in a try catch statement that looks for a keyword and if it does not find the keyword it will refresh every 5 seconds until this word is found.
def find_link_by_word_in_href(driver, words):
for word in words:
try:
return driver.find_element(By.XPATH, f"//*[contains(#href,'{word}')]")
except NoSuchElementException:
pass
while True:
element = find_link_by_word_in_href(driver, ['dadsa', 'daasd', 'asdsad'])
if element is not None:
element.click()
break
else:
driver.refresh()
time.sleep(5)
I wish to break this once it hits 5 refreshes and restart the script choosing a new IP from the text file. Can anybody help or point me in the correct direction to solve this?

Protractor get console variable value

In Firefox browser developer tool console, I type var a = libraryform.firstname
It returns the firstname value entered by user for that form.
I am new to protractor and selenium. How can I call library.firstname in protractor to get the value of fistname?
Use Javascript Executor to execute the javascript which you able to get them working in developer console against your application.
For e.g.,
this returns web-element which retrieved through javascript executed in browser.
var element = browser.executeScript("document.getElementById('identifier1')");
Refer this page for more examples
If this single line returns required value in browser console, then the protractor code should be some thing like this,
var name = browser.executeScript('return libraryform.firstname')
If you need too many lines of js, you can specify with semicolon separated,
var name = browser.executeScript("var element=$('.dropdown-toggle').eq(0); return element.text();")

Robot Framework: How to make Wait until keyword from selenium library return true or false

I would like to Run a keyword if an element is present on page.
I try using Selenium library's Wait Until Page Contains Element keyword, but it will always return "None", whether or not the element is present. I tried setting custom error, but that won't work either:
${condition} = Wait Until Page Contains Element ${locator} timeout=5 error=false
Run Keyword if '${condition}'=='false' click element ${some_refreshButton_locator}
Keyword click element ${locator} will run only when I make condition '${condition}'=='None'
Am I doing something wrong and how can I make a Selenium Library Wait until... keyword return true or false.
Thanks!
Wait Until Page Contains Element does not return anything but will raise error if element is not found. One workaround is to wrap Run Keyword And Ignore Error around it.
*** Settings ***
Library Selenium2Library
*** Test Cases ***
Test wait
Open Browser http://www.google.com/ gc
${result} ${condition}= Run Keyword And Ignore Error Wait Until Page Contains Stackoverflow timeout=2 error=false
Run Keyword if '${condition}'=='false' Log clicking
Close All Browsers
"Run Keyword And Return Status" can also be used to get True/False status as below:
${condition} = Run keyword And Return Status Wait Until Page Contains Element ${locator} timeout=5 error=false

Selenium's WebDriver.execute_script() returns 'None'

My program is having trouble getting an existing class from a webpage using Selenium. It seems that my WebDriver.execute_script function is not working.
import urllib
from selenium import webdriver
#Path to the chromedriver is definitely working fine.
path_to_chromedriver = 'C:\Users\Ben\Desktop\Coding\FreeFoodFinder\chromedriver.exe'
browser = webdriver.Chrome(executable_path = path_to_chromedriver)
url = 'http://www.maidservicetexas.com/'
browser.implicitly_wait(30)
browser.get(url)
content = browser.execute_script("document.getElementsByClassName('content')");
#Just printing the first character of the returned content's toString for now. Don't want the whole thing yet.
#Only ever prints 'N', the first letter of 'None'...so obviously it isn't finding the jsgenerated content even after waiting.
print content
My program returns 'None,' which tells me that the javascript function is not returning a value/being executed. Chrome's web dev tools tell me that 'content' is certainly a valid class name. The webpage isn't even dynamically generated (my eventual goal is to scrape dynamic content, which is why I make my WebDriver wait for 30 seconds before running the script.)
Return the value:
content = browser.execute_script("return document.getElementsByClassName('content');");

Explicit in selenium

I am using Selenium to get div value, but the fallowing code is not waiting for the page, just for URL. I used time.sleep, which is very primitive and totally not flexible. I want to change it on the explicit, but I am not too experienced in Python and I have a problem with that.
The website name has been changed just in case :
def repeat():
import wx
while True:
botloc = driver.find_element_by_id('botloc').text
print botloc
botX,botY = map(int,botloc.split(','))
print botX
print botY
wx.Yield()
def checker():
if driver.current_url == 'logged.example.com':
time.sleep(5)
repeat()
else:
checker()
checker()
How can I replace time.sleep with something flexible to wait the shortest time as possible after the page will be loaded? How to use explicit correctly with my code?
I know that's possible with using an element from the website, but I can't write anything sensible, I just need an example.
Is possibility to use element_by_id('botloc') for wait till it will be visible then start repeat() ?
How can i replace time.sleep with something flexible to wait shortest
time as possible after the page will be loaded?
I suppose you use get(url) to load the page. Generally you don't have to do anything, WebDriver automatically waits until page is being loaded. So you can remove time.sleep(). However there are some issues reported when loading the page using get with firefox driver, because of that you will have to wait for some target element which is supposed to be in the loaded page as mentioned below.
How to use explicit correctly with my code?
Have you checked Selenium webdriver documentation ? you can wait for botloc element explicitly as below
//assuming you have a valid webdriver reference
//Ex: DEFAULT_WAIT = 10 means
//waits up to 10 seconds before throwing a TimeoutException or if it finds the element will return it in 0 - 10 seconds.
element = WebDriverWait(webdriver, DEFAULT_WAIT).until(EC.presence_of_element_located((By.ID, "botloc")))
Refer this page for more information