Python Selenium Can not find ID inside Div - python

So I am trying to automate my daily thing to login to my ADP website but it seems whatever function I pick from selenium is does not find the ID that I am looking for which is "id="login-form_username".
Below is the part of the Selenim Python code.
user_textbox=driver.find_element_by_id('login-form_username')
user_textbox.send_keys(user)
user_textbox.click()
user_confirm=driver.find_element_by_id("verifUseridBtn")
user_pass_textbox=driver.find_element_by_id("login-form_password")
user_signin=driver.find_element_by_id("signBtn")
user_signin.send_keys(Keys.RETURN)
Also Attached screenshot of the site and error from my IDE. Im not sure if its because for security reasons that the Website I am trying to access wont allow this kind of thing.
Thanks
Joel

Since you didn't share the page link and not the error you seeing we can only guess what is wrong with your code.
So it can be:
You are missing a wait / delay before accessing that element.
In this case something like this should resolve your issue:
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
driver = webdriver.Chrome(executable_path='chromedriver.exe')
wait = WebDriverWait(driver, 20)
driver.get("https://your_site_url/")
user_textbox = wait.until(EC.visibility_of_element_located((By.ID, "login-form_username")))
user_textbox.send_keys(user)
user_textbox.click()
Alternatively you can simply add a delay before locating the elements, something like this:
time.sleep(5)
user_textbox=driver.find_element_by_id('login-form_username')
user_textbox.send_keys(user)
user_textbox.click()
time.sleep(5)
user_confirm=driver.find_element_by_id("verifUseridBtn")
user_pass_textbox=driver.find_element_by_id("login-form_password")
time.sleep(5)
user_signin=driver.find_element_by_id("signBtn")
user_signin.send_keys(Keys.RETURN)
but this is not the best approach.
2) Maybe there is an iframe there.
In this case you have to switch to that iframe.
3) Maybe you didn't set web driver to large enough size - if so please do that.
4) Possibly you have to scroll that input element into the view?

Related

python selenium: clicking buttons with various html element/css attributes

i'm trying to create python automation scripts using selenium to make appointment times, however i'm having some trouble with clicking on certain elements. to confirm an appointment the steps are going to the webpage > selecting the date > selecting the time > hitting confirm
this is what i've got so far:
driver = webdriver.Chrome()
driver.get('<url.date>') -- this works
driver.find_element(By.ID,'corresponding time id').click() -- this works
driver.find_element(By.DATA-TEST-ID,'order_summary_page-button-book').click() -- this is where i'm struggling
i've also tried doing it by the button class, using the css selector and looking for 'Reserve Now' text, and using the xpath
when i do it by xpath, i get the error NoSuchElementException
i tried adding a webdriver wait function but that didn't seem to work either.
the appointment times are really hard to get, so i'd rather not but any wait times into the code so it can execute and book as soon as slots open
here are the elements:
https://i.stack.imgur.com/S62kR.png
any help is appreciated!
Selenium doesn't have option to identify the element by DATA-TEST-ID locator.
In this case You have to use either by xpath or css selector
driver.find_element(By.XPATH,'//button[#data-test-id="order_summary_page-button-book"]').click()
OR
driver.find_element(By.CSS_SELECTOR,'button[data-test-id="order_summary_page-button-book"]').click()
Ideally you should use explicit wait to click on the element.
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//button[#data-test-id="order_summary_page-button-book"]'))).click()
you need following imports.
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

Issues with Selenium and a Single Page Application (Ember.js)

I just started trying to learn to write python code today. I am trying to make a web scraper to do some stuff, but i am having trouble getting selenium to click on the specific boxes/places to get to the correct page. Because it uses Ember.js, it is a Single Page Application. This throws things off a little. I found some stuff on stackoverflow about it, but i wasnt able to get it to work. I keep getting the "NoSuchElementException" error. Here is my code
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 = r'C:\Users\dme03\Desktop\desktop\webdrivers\chromedriver_win32\chromedriver.exe'
driver = webdriver.Chrome(PATH)
driver.get('https://thedailyrecord.com/reader-rankings-interface/')
time.sleep(15)
mim = driver.find_element(By.XPATH, '//img[#src="//media.secondstreetapp.com/1973100?width=370&height=180&cropmode=Fill&anchor=Center"]/parent::div')
mim.click
For the XPATH, I had to select a child element and specify the parent to select the correct element. The key identifiers in the parent element kept changing, so i was unable to select just that. I believe the XPATH is correct because when I put that into the website, it showed up as the correct line.
I have also tried using the code below, as i have seen some people suggest. When using that, I get a timed out error.
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//img[#src="//media.secondstreetapp.com/1973100?width=370&height=180&cropmode=Fill&anchor=Center"]/parent::div')))
Any help would be appreciated
NEW CODE for iFrames
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//iframe[#src="https://embed-882706.secondstreetapp.com/embed/ae709978-d6d1-499e-9f5e-136cc36eab1b/"]')))
mim = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//img[#src="//media.secondstreetapp.com/1973100?width=370&height=180&cropmode=Fill&anchor=Center"]/parent::div')))
mim.click
I am having issues clicking the item. To be fair, i am not sure that you are supposed to click that element to change the webpage. That is probably an issue.
Edit: nvm im just dumb that totally worked lol :))
You are getting "NoSuchElementException" error because the desired element is inside the iframe .So you have to switch to iframe at first.See the below image screenshot

Automate Login with Selenium and Python

I am trying to use Selenium to log into a printer and conduct some tests. I really do not have much experience with this process and have found it somewhat confusing.
First, to get values that I may have wanted I opened the printers web page in Chrome and did a right click "view page source" - This turned out to be not helpful. From here I can only see a bunch of <script> tags that call some .js scripts. I am assuming this is a big part of my problem.
Next I selected the "inspect" option after right clicking. From here I can see the actual HTML that is loaded. I logged into the site and recorded the process in Chrome. With this I was able to identify the variables which contain the Username and Password. I went to this part of the HTML did a right click and copied the Xpath. I then tried to use the Selenium find_element_by_xpath but still no luck. I have tried all the other methods to (find by ID, and name) however it returns an error that the element is not found.
I feel like there is something fundamental here that I am not understanding. Does anyone have any experience with this???
Note: I am using Python 3.7 and Selenium, however I am not opposed to trying something other than Selenium if there is a more graceful way to accomplish this.
My code looks something like this:
EDIT
Here is my updated code - I can confirm this is not just a time/wait issue. I have managed to successfully grab the first two outer elements but the second I go deeper it errors out.
def sel_test():
chromeOptions = Options()
chromeOptions.add_experimental_option("useAutomationExtension", False)
browser = webdriver.Chrome(chrome_options=chromeOptions)
url = 'http://<ip address>/'
browser.get(url)
try:
element = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '//*[#id="ccrx-root"]')))
finally: browser.quit()
The element that I want is buried in this tag - Maybe this has something to do with it? Maybe related to this post
<frame name="wlmframe" src="../startwlm/Start_Wlm.htm?arg11=">
As mentioned in this post you can only work with the current frame which is seen. You need to tell selenium to switch frames in order to access child frames.
For example:
browser.switch_to.frame('wlmframe')
This will then load the nested content so you can access the children
Your issue is most likely do to either the element not loading on the page until after your bot searches for it, or a pop-up changing the xpath of the element.
Try this:
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
from selenium.common.exceptions import TimeoutException
delay = 3 # seconds
try:
elementUsername = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.xpath, 'element-xpath')))
element.send_keys('your username')
except TimeoutException:
print("Loading took too much time!")
you can find out more about this here

Why my selenium IDE works good while the same python command doesn't?

I have some problems trying to automate repetitive tasks in my company's intranet website.
While I can properly find elements in other websites (clicking, sending keys etc.), I can't do the same in this website (the error I always get is "unable to find element with xpath == ... ").
I tried to do the same tasks recording them with Selenium IDE, and it works well.
If I export the Selenium IDE test case as python code and I compare the xpath command with mine, the xpath is the same, and it's the same that I get using the firefox selector to the point I want to click.
Since for other reasons I must work with explorer (so I have the explorer webdriver etc.), I haven't tried to run the code with python+(webdriver for Firefox).
Code:
from selenium import webdriver
import time
driver=webdriver.IE("D:\myfolder\IEDriverServer.exe")
driver.implicitly_wait(30)
base_url="https://companywebsite.ext")
driver.get(base_url) #it's ok until now, I can open the webpage
time.sleep(20)
element=driver.find_element_by_xpath("/html/body/div[2]/div/table/tbody/tr[3]")
time.sleep(10)
Here are my questions:
1) Why can I do things right in other websites and not in this?
2) Why Selenium IDE is able to do that and, with the same commands, it doesn't work with my python script?
3) Do I have to do particular setups?
Thank you very much.
You should try using wait's:
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 as EC
driver=webdriver.IE("D:\myfolder\IEDriverServer.exe")
driver.implicitly_wait(30)
base_url="https://companywebsite.ext")
driver.get(base_url)
wait = WebDriverWait(driver, 10)
element = wait.until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[2]/div/table/tbody/tr[3]")))
Hope this helps!

Selenium can't find any elements on IE11 once a dashboard is loaded

So something interesting is happening - unfortunately, I cannot post a url or something you guys can access, so maybe someone just has an idea of what the cause could be. I'd assume it would be something in development. Either way...
I have a test that logs in to an application, which will then load up a dashboard and search for some data. In Firefox, everything works fine. However, IE11 is having an issue once it logs in. So, Selenium works fine up until it loads the dashboard, where it will then find NOTHING. Not even "//html". It gives the basic element not found error. I'm assuming the developers have written something that prevents access, but the one I talked to wasn't sure.
Any idea of what could be causing this so that I could go suggest something to the developers? Again, this doesn't happen in Firefox, which could also suggest something's up with IE11 (though Selenium works fine on other applications and up until the Dashboard screen on this one). If you need more information, let me know - just be aware that I might not have much to give.
A couple things to note:
1. it is https protocol, but so is the login page
2. at the end of the url bar, it says there is a certificate error that will not go away
no matter what I do (I have added the supposedly correct certificates).
What I'm using:
Selenium 2.4.5
32 bit IEDriverServer
Python 2.7.8
IE11 (other IE versions are not supported, so I cannot compare)
Windows 7
EDIT:
I talked with a developer, and the best thing he could think of was the fact that the dashboard heavily uses polyfills, and that maybe that is the reason that Selenium isn't finding anything. With that being said, is there a way to find elements using Selenium when polyfills are involved? Or is Selenium just not going to work for it.
MORE:
Here is some code that equivalent to what my test would do. Assume that every exception would normally be caught and wouldn't kill the script...
from pdb import set_trace
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver
url = 'server.url'
capabilities = webdriver.DesiredCapabilities().INTERNETEXPLORER
capabilities['acceptSslCerts'] = True
# use path due to having both a 64 bit and 32 bit version of the driver - specifies which to use
driver = webdriver.Ie("path-to-iedriver.exe", capabilities=capabilities)
driver.maximize_window()
driver.get("https://%s" % url)
# click ie's continue to website
driver.get("javascript:document.getElementById('overridelink').click();")
time.sleep(1)
# clicks 'I Agree' on a page before login
driver.find_element_by_xpath("//button[text()='I Agree']").click()
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'username')))
# logs in
driver.find_element_by_id("username").send_keys("username")
driver.find_element_by_name("j_password").send_keys("password")
driver.find_element_by_xpath("//button[contains(text(), 'Log on')]").click()
# click continue when a successful login box pops up
ret = WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.ID, "continueBtn")))
ret.click()
# this waits for an element on the dashboard page - it will fail
WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.XPATH, "//span[contains(text(), 'banner')]")))
# everything from here on will fail due to element not found (only in IE)
driver.find_element_by_xpath("//html")
driver.find_element_by_xpath("anything")
set_trace()
Again, the elements will be found in Firefox and Chrome, but not IE.
You might need to add an Explicit Wait to wait for the data to be loaded. For example, you may wait for a specific element to become present:
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 as EC
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "myDynamicElement"))
)
# proceed with locating the elements containing the desired data
Old answer
It sounds like this is a certificate issue, print out the .title and the .page_source once the page is loaded and check what is there.
You can also let it accept the certificate by setting acceptSslCerts desired capability to True:
from selenium import webdriver
capabilities = webdriver.DesiredCapabilities().INTERNETEXPLORER
capabilities['acceptSslCerts'] = True
driver = webdriver.Ie(capabilities=capabilities)
# ...
Also see:
How to deal with certificates using Selenium?

Categories

Resources