How to navigate to next page using selenium? - python

So im trying to navigate to a web page after signing in, and when i execute the code, it signs in but never navigates to the next page. I have tried navigating to the next page in the same function, and in seperate, but it doesn't move onto the next one. On MacOS Big Sur using Pycharm, python 3.9.
*I left out the authentication code, but cannot get the driver to navigate to a page after the sign in page.
driver.get('https://www.amazon.com/ap/signin?openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2F%3Fref_%3Dnav_signin&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=usflex&openid.mode=checkid_setup&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&')
driver.get('https://www.amazon.com/gp/product/B071FXZBMV/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1')
def amazon_login_buy():
driver.get('https://www.amazon.com/ap/signin?openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2F%3Fref_%3Dnav_signin&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=usflex&openid.mode=checkid_setup&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&')
# enter email here
driver.find_element_by_id('ap_email').send_keys('')
driver.find_element_by_id('continue').click()
# enter password here
driver.find_element_by_id('ap_password').send_keys('')
driver.find_element_by_name('rememberMe').click()
driver.find_element_by_id('signInSubmit').click()
def amazon_navigate():
# driver.implicitly_wait(2)
driver.get('https://www.amazon.com/gp/product/B071FXZBMV/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1')
amazon_login_buy()
amazon_navigate()

Here is how you can navigate to that item (minus all the log in stuff), without using multiple get calls.
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.webdriver.common.keys import Keys
def amazon_login_buy():
wait = WebDriverWait(driver,10)
driver.get('https://www.amazon.com')
textbox = driver.find_element_by_id("twotabsearchtextbox")
textbox.send_keys("OHill Cable Clips,16 Pack Black Adhesive Cord Holders, Ideal Cable Cords Management for Organizing Cable Wires-Home, Office, Car, Desk Nightstand")
textbox.send_keys(Keys.ENTER)
item = wait.until(EC.element_to_be_clickable((By.XPATH,"(//div[#data-component-type='s-search-result'])[3]//img")))
item.click()
amazon_login_buy()
You could probably get away with a shorter search word as well for send_keys, you'd have to play around with that.
Basically it will:
Go to Amazon.com
Search for the item you want in the search box
Click the image of the item from the resulting list of items. I was guessing which one you actually wanted, you can experiment with that piece. I chose the 1st one down that wasn't "sponsored".

Related

I can't close a dialog box when I using Selenium and Python

I'm using Selenium to collect data when a message window pops up, this window is just a manual so I want to turn it off. I have tried many ways like: find element by id, xpath,... it doesn't work, or even switch to alert doesn't work. Below is the html screenshot of the website.
I appreciate all of your guidance.
enter image description here
As per the screen shot, span tag has class with close-btn. I am GUESSING that clicking on that Element would close the pop-up. Try below lines once. Have also applied Explicit Waits incase if the pop-up takes time to load.
#imports required:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
wait = WebDriverWait(driver,30)
#Trial 1:
#driver.find_element_by_xpath("//div[#class='modal=dialog']//span[conatins(#class,'close-btn')]").click()
wait.until(EC.element_to_be_clickable((By.XPATH,"//div[#class='modal=dialog']//span[conatins(#class,'close-btn')]"))).click()
# Can also use `ends-with` instead of `contains` in the xpath.
#Trial -2
#driver.find_element_by_xpath("//div[#role='dialog']//span(contains(#class,'close-btn'))").click()
wait.until(EC.element_to_be_clickable((By.XPATH,"//div[#role='dialog']//span(contains(#class,'close-btn'))"))).click()

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

Can't deselect a already selected blank space using selenium

I've written some code in python in combination with selenium to reach the target page where the data I'm after is located. My below code can almost get there. I just need a little twitch on it to make it work flawlessly.
Firstly, the browsers leads to a page where a default Login button located under Public User Login title then it clicks on that button. When a new page appears, It clicks on the Advanced tab located on the top of that page under the title of Account Search. Upon clicking on that tab there is a list of items visible under the title of Parcel Classification. Now, I need to choose 02.C - PROPERTY BURDENED BY CONSERVATION EASEMENTS from options and press the search button. That's it.
My scraper can do the whole thing accordingly but the problem is: when it selects the preferable option (I've mentioned the text above), there is another option (the very first blank space of those options) by default remains selected. That is why, when my scraper clicks on the search button, it populates wrong results (a page with no results).
How can I deselect the first blank space and continue on with the rest. Thanks in advance for taking a look into it.
The link to that webpage: Web_Link
This is what I've tried so far:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
driver.get("replace_with_above_link")
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#middle_left input[name='submit']"))).click()
wait.until(EC.presence_of_element_located((By.LINK_TEXT, "Advanced"))).click()
Select(wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,".tableHtmlLayout #accountTypeID")))).select_by_visible_text('02.C - PROPERTY BURDENED BY CONSERVATION EASEMENTS')
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".buttons input[type='submit']"))).click()
driver.quit()
If i understand the problem correctly, you want to deselect the first option before you select your target option? How about doing something like this?
wait.until(EC.presence_of_element_located((By.LINK_TEXT, "Advanced"))).click()
account_selector = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,".tableHtmlLayout #accountTypeID")))
Select(account_selector).deselect_by_index(0)
Select(account_selector).select_by_visible_text('02.C - PROPERTY BURDENED BY CONSERVATION EASEMENTS')
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".buttons input[type='submit']"))).click()

Cannot find element from a jump out window. How can I switch to a new jump out window?

I'm trying to automate our system with Python2.7, Selenium-webdriver, and Sikuli. I have a problem on login. Every time I open our system, the first page is an empty page, and it will jump to another page automatically; the new page is the main login page, so Python is always trying to find the element from the first page. The first page sometimes shows:
your session has timeout
I set a really large number for session timeout, but it doesn't work.
Here is my code:
import requests
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome()
driver.get('http://172.16.1.186:8080/C******E/servlet/LOGON')
# time.sleep(15)
bankid = driver.find_element_by_id("idBANK")
bankid.send_keys(01)
empid = driver.find_element_by_id("idEMPLOYEE")
empid.send_keys(200010)
pwdid = driver.fin`enter code here`d_element_by_id("idPASSWORD")
pwdid.send_keys("C******e1")
elem = driver.find_element_by_id("maint")
elem.send_keys(Keys.RETURN)
First of all, I can't see any Sikuli usage in your example. If you were using Sikuli, it wouldn't matter how the other page was launched as you'd be interacting with whatever is visible on your screen at that time.
In Selenium, if you have multiple windows you have to switch your driver to the correct one. A quick way to get a list of the available windows is something like this:
for handle in driver.window_handles:
driver.switch_to_window(handle);
print "Switched to handle:", handle
element = browser.find_element_by_tag_name("title")
print element.get_attribute("value")

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