How to find element using Selenium and Chromedriver - python

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

Related

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

Select a textbox with Python Selenium, changing frame

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')

JavaScript works on chrome console but doesn't work in firefox or selenium code

I am trying to automate the login to the following page using selenium:
https://services.cal-online.co.il/Card-Holders/SCREENS/AccountManagement/Login.aspx?ReturnUrl=%2fcard-holders%2fScreens%2fAccountManagement%2fHomePage.aspx
Trying to find the elements of username and password using both id, css selector and xpath didn't work.
self._web_driver.find_element_by_xpath('//*[#id="txt-login-username"]')
self._web_driver.find_element_by_id("txt-login-password")
self._web_driver.find_element_by_css_selector('#txt-login-username')
For all three I get NoSuchElement exception
I tried the following JS script: document.getElementById('txt-login-username')
when I run this script in selenium or in firefox it returns Null
but when I run it in chrome console I get a result I can use.
Is there any way to make it work from the python code or to run this on the chrome console itself and not from the python execute_script?
To automate the login to the page using Selenium as the the desired elements are 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 the following solution:
Code Block:
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
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
driver = webdriver.Chrome(options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://services.cal-online.co.il/Card-Holders/SCREENS/AccountManagement/Login.aspx?ReturnUrl=%2fcard-holders%2fScreens%2fAccountManagement%2fHomePage.aspx")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='calconnectIframe']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='txt-login-username']"))).send_keys("ariel6653")
driver.find_element_by_xpath("//input[#id='txt-login-password']").send_keys("ariel6653")
Browser Snapshot:
Here you can find a relevant discussion on Ways to deal with #document under iframe
found a solution to the problem. the problem really was that the object is inside an iframe.
I tried to use the solution suggested in Get element from within an iFrame
but got a security error. the solution is to switch frame the follwoing way:
driver.switch_to.frame("iframe")
and now you can use the normal find elment

How to find the Xpath of the element as per the html through Selenium and Python

Tried something like this in python unable I want to click on cross using Selenium.
driver.find_element_by_xpath("//span[contains(#onclick, 'parent.$WZRK_WR.closeIframe('60005','intentPreview');')]").click()
As per the HTML you have shared to click on the element depicted as X, first you need to induce WebDriverWait while switching to the <iframe> and again induce WebDriverWait for the desired element to be clickable and you can use the following solution:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='wiz-iframe-intent']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='CT_Interstitial']//span[#class='CT_InterstitialClose']"))).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
Open below URL and Click on Add to Chrome button.
Xpath Helper- https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl?hl=en
Once plugin added to Chrome. Open your application/webpage and press SHIFT+CTRL+X. Black window will appear on top then you can play with xpath.
If xpath is invalid- then you will get xpath error and if no matches found then it will display NULL.
Note- To check element attribute still you can use F12 that is nothing but default inspect element for all browser then check attribute and create your own xpath and try.
Xpath Syntax
// tagname[#attribute-name=’value1′] and if you are not sure about tag name then no worry you can try with * also
//*[#attribute-name='value1']
You are dealing with an iframe. Follow below steps :
You'll need to switch control to iframe.
Then perform your action (in this case 'Click close').
Switch the control back to default frame.
You can try with this css_selector :
div.CT_InterstitialContents+span.CT_InterstitialClose[onclick]
Xpath would be :
//div[#class='CT_InterstitialContents']/following-sibling::span[#class='CT_InterstitialClose' and onclick]
Try to use this xPath:
//div[#id = 'contentDiv']/div/div/span[#class = 'CT_InterstitialClose']
Code:
close_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[#id = 'contentDiv']/div/div/span[#class = 'CT_InterstitialClose']")))
close_btn.click()
Imports:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
Explanation:
WebDriverWait is used to wait until element will be clickable, and only then clicks on it. In this example WebDriverWait will wait at least 10 seconds until element will be clickable.
PS: as I see in the screenshot your element is probably in iframe. That means you have to switch to this iframe first, to be able to interact with the elements in it. The code sample for it would be like this:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "XPATH_TO_FRAME")))
# do stuff
close_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[#id = 'contentDiv']/div/div/span[#class = 'CT_InterstitialClose']")))
close_btn.click()
# switch back to default content
driver.switch_to.default_content()
You can write xpath using class name of span. Please refer an example below.
//span[#class='amountCharged']

Selenium can't locate the element, a search button using xpath

I can't find a solution how this element cannot be found by using a selenium xpath. Other button on other websites always working just fine. Usually what I would normally do is, just open up a page and inspect the element and I would right click it and copy the xpath. Done.
But this website, www.gsc.com.my (a malaysian cinema booking site). Seems not able to find the button. Is it protected by another security layer?
Lets see the code below,
from selenium import webdriver
chromedriver_path = './chromedriver.exe'
driver = webdriver.Chrome(chromedriver_path)
driver.get('https://www.gsc.com.my')
driver.find_element_by_xpath("""//*[#id="btnNext"]""").click()
The error Message:
no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="btnNext"]"}
Button is located inside an iframe, so you need to switch to that frame before clicking the button:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
driver.switch_to.frame('getQuickFilter_ctrl_ifrBox')
wait(driver, 10).until(EC.element_to_be_clickable((By.ID, "btnNext"))).click()
Because there are two elements with id btnNext, you'll have to specify which of them using an index, 1 for the first, 2 for the second.
driver.find_element_by_xpath("""//*[#id="btnNext"][1]""").click()
You can try with this css selector :
div.container input#btnNext
Note that you will need to switch to iframe first , cause the check button is in a iframe.
For switching to iframe :
driver.switch_to.frame(driver.find_element_by_id("getQuickFilter_ctrl_ifrBox"))
and for clicking on check Button
wait = WebDriverWait(driver, 10)
check_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.container input#btnNext")))
check_button.click()

Categories

Resources