Select a textbox with Python Selenium, changing frame - python

Warning: Complete Novice
I'm trying to select the text box for the 'Login ID' on the website:
https://www.schwab.com/public/schwab/nn/login/login.html&lang=en
I'm using Python 3.7 and Selenium. I've tried to inspect the textbox element to find the CSS Selector. This causes an error. I know I need to switch my 'frame', but I can't read the website back end coding well enough to find the right frame name.
from selenium import webdriver
browser = webdriver.Chrome(executable_path=driver_path, chrome_options=option)
Login_Id_TextBox = browser.find_element_by_id('LoginId')
Can anyone help me find the right frame or direct me to a good source to learning more about them?

To send a character sequence with in the text box for the Login ID on the website https://www.schwab.com/public/schwab/nn/login/login.html&lang=en as the the desired element is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame_to_be_available_and_switch_to_it().
Induce WebDriverWait for the desired element_to_be_clickable().
You can use either of the following Locator Strategies:
CSS_SELECTOR:
driver.get('https://www.schwab.com/public/schwab/nn/login/login.html&lang=en')
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#loginIframe")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.form-control#LoginId"))).send_keys("averagejoe1080")
XPATH:
driver.get('https://www.schwab.com/public/schwab/nn/login/login.html&lang=en')
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='loginIframe']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='form-control' and #id='LoginId']"))).send_keys("averagejoe1080")
Here you can find a relevant discussion on Ways to deal with #document under iframe

Here is the simple code for your requirement, I test this successfully.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.schwab.com/public/schwab/nn/login/login.html&lang=en')
driver.switch_to_frame("loginIframe")
Login_Id_TextBox = driver.find_element_by_id('LoginId')

Related

How can I make Selenium click on a href by css selector?

I have been trying to make a Selenium bot that searches for a word on github, clicks on the first link and then downloads it with Python 3.8 and I got stuck with making the bot click on the hyperlink. I understand that I can make the bot click on it with driver.find_element(By.XPATH, "Xpath").click() but I want to be able to find the path of the href with another method for the sake of learning, in this case CSS_SELECTOR. Source code of the first hyperlink result is like this:
HTML:
Since every single result is under the same "a" selector with a class of "v-align-middle", I thought of using this code: driver.find_element(By.CSS_SELECTOR, ".v-align-middle").click() but it did not seem to work. What am I missing?
The desired element is a dynamic element, so to click() on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.v-align-middle[href='/asdf-vm/asdf'][data-hydro-click][data-hydro-click-hmac]"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='v-align-middle' and #href='/asdf-vm/asdf'][#data-hydro-click and #data-hydro-click-hmac]"))).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

Create a click event using selenium when there are multiple same class and no id elements using Python

I am trying to create an automated download event for one ERP platform using Selenium in python. This is a bit tricky for this part as there is no specific element to find and click.
Multiple buttons have the same class and no id for the highlighted button (DATABASE/BACKUP)
Can anyone help me with the same?
event flow >>
Login page > click "administration button" > click "database / backup"
thanks
As per Selenium documentation https://selenium-python.readthedocs.io/locating-elements.html you can find all of the elements by their class name and then access them by index ([index_number]).
all_buttons = driver.find_elements_by_class_name("your_class_name")
then you can do something like:
button_to_click = all_buttons[3] # The 4th button which has index 3 in all the retrieved buttons by class name
driver.find_element(By.XPATH,"//a[contains(text(),'Database / Backup')]").click()
Simply click the a tag which contains the text.
To click on the element with text as DATABASE/BACKUP you can use either of the following Locator Strategies:
Using xpath:
driver.find_element(By.XPATH, "//a[#class='menui' and contains(., 'DATABASE/BACKUP')]").click()
Using xpath:
driver.find_element(By.XPATH, "//a[text()[contains(., 'DATABASE/BACKUP')]]").click()
The desired element is a dynamic element, ideally to click on a clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='menui' and contains(., 'DATABASE/BACKUP')]"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[text()[contains(., 'DATABASE/BACKUP')]]"))).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

How to click the Continue button within https://in.indeed.com/ website using selenium in python?

I am trying to write a code that is able to auto apply on job openings on indeed.com. I have managed to reach the last stage, however, the final click on the application form is giving me a lot of trouble. Please refer the page as below
Once logged in to my profile, I go to the relevant search page, click on the listing I am interested in and then on the final page (shown above) I am trying to click on the continue button using xpath. For the previous step (also a page on the same form), the form had multiple iframes which I was able to switch successfully. Now for the current page, I am stuck as the click function does not do anything. I have written the following code:
driver.get("https://in.indeed.com/jobs?q=data%20analyst&l=Delhi&vjk=5c0bd416675cf4e5")
driver.find_element_by_xpath('//*[#id="apply-button-container"]/div[1]/span[1]').click()
time.sleep(5)
frame_1 = driver.find_element_by_css_selector('iframe[title="Job application form container"')
driver.switch_to.frame(frame_1)
frame_2 = driver.find_element_by_css_selector('iframe[title="Job application form"]')
driver.switch_to.frame(frame_2)
continue_btn = driver.find_element_by_css_selector('#form-action-continue')
continue_btn.click()
driver.find_element_by_xpath('//*[#id="form-action-continue"]').click()
I have tried switching the iframes again for this step but nothing happens. Even the .click() function does not do anything.
Will appreciate some help on this.
The element Continue is within nested <iframe> elements so you have to:
Induce WebDriverWait for the parent frame to be available and switch to it.
Induce WebDriverWait for the child frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://in.indeed.com/jobs?q=data%20analyst&l=Delhi&vjk=5c0bd416675cf4e5")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.indeed-apply-button-label"))).click()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[id^='indeedapply-modal-preload']")))
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='Job application form']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#form-action-continue"))).click()
Using XPATH:
driver.get("https://in.indeed.com/jobs?q=data%20analyst&l=Delhi&vjk=5c0bd416675cf4e5")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='indeed-apply-button-label']"))).click()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(#id, 'indeedapply-modal-preload')]")))
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#title='Job application form']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#id='form-action-continue']"))).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:
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element

How to locate an element within nested iframes using Selenium and Python

Trying to find the xpath for the <nobr> tag at the bottom of the page and it's driving me crazy. The frames are what is complicating the issue. Can someone please tell me the xpath for this?
I am trying to write a selenium script and I have gotten to:
EC.frame_to_be_available_and_switch_to_it, ((By.NAME, "main"))
After that none of my xpath attempts have worked to get the content in the <nobr> tag where it says supplier.
HTML:
Another option :
//nobr[.='Suppliers']/parent::span
For the frameset :
//frameset[#rows='48,*']
As the the desired element is within multiple neted <iframe> so to invoke click() on the element you have to:
Induce WebDriverWait for the great grand parent frame_to_be_available_and_switch_to_it().
Induce WebDriverWait for the grand parent frame_to_be_available_and_switch_to_it().
Induce WebDriverWait for the parent frame_to_be_available_and_switch_to_it().
Induce WebDriverWait for the desired element_to_be_clickable().
You can use the following solution:
Using CSS_SELECTOR:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#main[name='main']")))
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name='marketcenterHome']")))
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#main[name='TabNavigation']")))
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.tabSelText>nobr")))
Using XPATH:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='main' and #name='main']")))
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#name='marketcenterHome']")))
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#name='TabNavigation']")))
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='tabSelText']/nobr[text()='Suppliers']")))
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
Reference
You can find a couple of relevant discussions in:
Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome
Ways to deal with #document under iframe
Apologies for the delay in coming back with the solution but the problem was that I needed to go up a level from the title iframe to get myself into root and then I was able to go down into main.
A simple problem to have but not so simple when you don't know too much about frames at the time.
I appreciate everyone who took the time to answer.

How to find element using Selenium and Chromedriver

I am new to Selenium. I am using Selenium 3.141 from anaconda on OSX. My Chrome version is 71.0 and Chromedriver version is 2.45. My goal is to use Selenium to click on a button with "Accept" on it on a webpage. I am able to instantiate the webdriver object using the executable and use the same to load the page in question. I then have a wait for 20 seconds. It's the next bit that fails. Code is unable to find the element that has to be clicked on. Attached are two images of elements associated with the button at various levels and sublevels with highlighted parts from inspection. I have tried variants such as
accept_button = driver.find_element_by_class_name('flex-x.static.h- center.v-center.bt-button.filled')
and
accept_button = driver.find_element_by_class_name('Accept')
and a few others to no avail. Please help.
Button frame:
Accept rectangle:
The desired element is an Angular element so to locate/click you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
Using CSS_SELECTOR:
accept_button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "bt-button.Aceept.ng-isolate-scope[mutable-label='prelogin.translations.Accept']")))
Using XPATH:
accept_button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//bt-button[#class='Aceept ng-isolate-scope focused' and #mutable-label='prelogin.translations.Accept']")))
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
did you see bt-button.Accept.ng-isolate-scope.focused you can use it for selecting using css selector
accept_button = driver.find_element_by_css_selector('bt-button.Accept.ng-isolate-scope.focused')
# or
accept_button = driver.find_element_by_css_selector('bt-button.Accept')
I would suggest you to check if the element you trying to interact with is in iframe.
To identify this you can: Right click on the element, If you find the option like 'This Frame' then it is an iframe; or you can just search in page source for iframe tag to identify.
you can refer to this question for further solution if indded your element is part of iframe: Select iframe using Python + Selenium

Categories

Resources