Unable to locate element: while clicking a button through Selenium and Python - python

I want to click on a button (event) using Selenium on Python and the button code is:
<input id="workbenchLst:j_id_id509" name="workbenchLst:j_id_id509" onclick="A4J.AJAX.Submit('workbenchLst',event,{'similarityGroupingId':'workbenchLst:j_id_id509','parameters':{'ajaxSingle':'workbenchLst:j_id_id509','workbenchLst:j_id_id509':'workbenchLst:j_id_id509'} ,'containerId':'j_id_id1'} );return false;" value="Add" type="button" autocomplete="off">
My code:
driver.find_element_by_id("workbenchLst:j_id_id509").click()#add
and it isn't working, the error:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [name="workbenchLst:j_id_id509"]

Check for the iframe/frame in your page first, if there is a frame/iframe then you need to switch the frame first like below :
driver.switch_to_frame(driver.find_element_by_id("iframeid"));
You can try clicking on the element using below XPath:
element = driver.find_element_by_xpath("//input[contains(#id, 'workbenchLst') and #value='Add']");
element.click();
Or you can try using the JavaScript Executor like below :
element = driver.find_element_by_xpath("//input[contains(#id, 'workbenchLst') and #value='Add']");
driver.execute_script("arguments[0].click();", element);
Still not working then try to give some delay, import sleep from time like below :
from time import sleep
driver.switch_to_frame(driver.find_element_by_id("iframeid"));
sleep(5);
element = driver.find_element_by_xpath("(//input[contains(#id, 'workbenchLst') and #value='Add'])[2]");
element.click();
I hope it works...

The desired element is a dynamic element so to locate the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[id^='workbenchLst:'][name^='workbenchLst:'][value='Add']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[starts-with(#id,'workbenchLst:') and starts-with(#name,'workbenchLst:')][#value='Add']"))).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

Related

Unable to click label that doesn't have any text using Python and Selenium

I'm trying to click the label below.
Label html:
<div class="is-inline-block">
<input id="entradas-checkbox-condiciones" type="checkbox" class="switch is-rounded is-small">
<label style="padding-left:1rem;">
I've tried clicking the label with this code:
WebDriverWait = wait
label1 = wait(self.driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, "//*[#class = 'is-inline-block']")))[3]
label1.find_element_by_xpath('//label[style="padding-left:1rem;"]').click()
But i get the following error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:
Can somebody help me with the code?
Thanks in advance.
To locate and invoke click() on the clickable element instead of presence_of_element_located() 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(self.driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.is-inline-block input.switch.is-rounded.is-small#entradas-checkbox-condiciones"))).click()
Using XPATH:
WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='is-inline-block']//input[#class='switch is-rounded is-small' and #id='entradas-checkbox-condiciones']"))).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
References
You can find a couple of relevant discussions on NoSuchElementException in:
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

Not able to click a link button using selenium gives error element not found exception

I am trying to click on the ARCGIS button on webpage https://gisapr.atco.com/portal/home/signin.html?returnUrl=https%3A%2F%2Fgisapr.atco.com%2Fportal%2Fhome%2F
But getting no element found or timeout errors.
I'm also unable to access inside login form contents but no luck.
Tried:
Implicit and explicit waits
Time sleep()
by all finds..
The element ARCGIS 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:
Using CSS_SELECTOR:
driver.get('https://gisapr.atco.com/portal/home/signin.html?returnUrl=https%3A%2F%2Fgisapr.atco.com%2Fportal%2Fhome%2F')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#oAuthFrame")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn_wide#ago_Name"))).click()
Using XPATH:
driver.get('https://gisapr.atco.com/portal/home/signin.html?returnUrl=https%3A%2F%2Fgisapr.atco.com%2Fportal%2Fhome%2F')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='oAuthFrame']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#id='ago_Name' and text()='ArcGIS']"))).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

Type in a textbox via python: unable to locate element

I'm trying to locate an element using python selenium, and have the html below:
<input class="form-control" type="text" placeholder="University Search">
I couldn't locate where to type what I want to type.
from selenium import webdriver
import time
driver = webdriver.Chrome(executable_path=r"D:\Python\Lib\site-packages\selenium\chromedriver.exe")
driver.get('https://www.topuniversities.com/university-rankings/university-subject-rankings/2020/engineering-technology')
#<input class="form-control" type="text" placeholder="University Search">
text_area = driver.find_element_by_name('University Search')
text_area.send_keys("oxford university")
You are attempting to use find_element_by_name and yet this element has no name attribute defined. You need to look for the element with the specific placeholder attribute you are interested in - you can use find_element_by_xpath for this:
text_area = driver.find_element_by_xpath("//input[#placeholder='University Search']")
Also aside: When I open my browser, I don't see an element with "University Search" in the placeholder, only a search bar with "Site Search" -- but this might be a regional and/or browser difference.
Make sure you wait for the page to load using webdriver waits,click the popup and then proceed to target the element to send keys to.
driver.get('https://www.topuniversities.com/university-rankings/university-subject-rankings/2020/engineering-technology')
driver.maximize_window()
wait=WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='OK, I agree']"))).click()
text_area=wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR ,"td.uni-search.uni.sorting_disabled > div > input")))
text_area.send_keys("oxford university")
Imports
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Element to target
<input class="form-control" type="text" placeholder="University Search">
To send a character sequence to the element you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element_by_css_selector("input.form-control[placeholder='University search']").send_keys("oxford university")
Using xpath:
driver.find_element_by_xpath("//input[#class='form-control' and #placeholder='University search']").send_keys("oxford university")
Ideally, to send a character sequence to 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:
driver.get("https://www.topuniversities.com/university-rankings/university-subject-rankings/2020/engineering-technology")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.form-control[placeholder='University search']"))).send_keys("oxford university")
Using XPATH:
driver.get("https://www.topuniversities.com/university-rankings/university-subject-rankings/2020/engineering-technology")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='form-control' and #placeholder='University search']"))).send_keys("oxford university")
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:
References
You can find a couple of relevant discussions on NoSuchElementException in:
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
Very close, try the XPath when all else fails:
text_area = driver.find_element_by_xpath("//*[#id='qs-rankings']/thead/tr[3]/td[2]/div/input")
You can copy the full/relative XPath to clipboard if you're inspecting the webpage's html.

NoSuchElementException: Message: Unable to locate element while trying to locate an element using Selenium and Python

I have a specific login button I can't access with Python Firefox Selenium. It's the login button on this webpage: https://schalter.asvz.ch/tn/lessons/39616
I'm running Ubuntu 16.04, Python 3.5.2, Firefox 65.0 and Selenium 3.141.
I tried several combinations of approaches I found here on stackoverflow including the following:
login = driver.find_element_by_xpath("//*[#class='btn btn-default ng-star-inserted']")
login = driver.find_element_by_xpath("//button[#class='btn btn-default ng-star-inserted']")
login = driver.find_element_by_class_name('btn btn-default ng-star-inserted')
login = driver.find_element_by_xpath("//*[contains(., 'Login')]")
login = driver.find_element_by_name('app-lessons-enrollment-button')
But none of them worked. Always resulting in:
NoSuchElementException: Message: Unable to locate element:
//*[#class='btn btn-default ng-star-inserted']
What is so different with this button? How can I make it work?
This error message...
NoSuchElementException: Message: Unable to locate element: //*[#class='btn btn-default ng-star-inserted']
...implies that the ChromeDriver was unable to locate the desired element through the locator you have used.
Virtually, your first two(2) locators were just perfecto.
However, the desired element is an Angular element so to locate the element you have 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, "button.btn.btn-default.ng-star-inserted[title='Login']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-default ng-star-inserted' and #title='Login']"))).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
Try the below option.
Login=driver.find_element_by_css_selector("button.ng-star-inserted")
Or try this
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'button.ng-star-inserted'))).click()
You need following imports for option 2.
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
Following xpath works just fine (tested using selenium java)
//button[#title='Login']
I was able to locate and click the button.
Try the below xpath:
xpath = "//button[#title='Login']"
element = driver.find_element_by_xpath(xpath);
element.click();

Selenium and Python: Trouble with clicking a button with "data-disable-with" attribute

[edit: added screenshot of Chrome developer view below...]
I'm trying to click on this object:
<input type="submit" name="commit" value="Load Report" class="button" data-disable-with="Load Report">
In the UI, the button is clickable until it gets clicked to start the report. It is then disabled until the report loads.
But when I make the call in code:
driver.find_element_by_name("commit").click()
it throws an exception:
ElementNotVisibleException: element not interactable
(Session info: chrome=71.0.3578.98)
(Driver info: chromedriver=2.45.615355 (d5698f682d8b2742017df6c81e0bd8e6a3063189),platform=Mac OS X 10.14.0 x86_64)
So, Im pretty sure I found the right button (unless there is another one named "commit"), but for some reason it is not clickable. There are no discernible objects in front of it, but maybe something hiding in the CSS or ...? I'm a know-nothing-noob. Any hints?
The desired element is a dynamic element so to invoke click() on the element you need to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.button[name='commit'][value='Load Report']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='button' and #name='commit'][#value='Load Report']"))).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
You can use the explicit wait to element to be visible
System.setProperty("webdriver.chrome.driver", "path of chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("URL");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
WebElement commitBtn = driver.findElement(By.name("commit"));
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOf(commitBtn));
commitBtn.click();
or you can use javascriptexecurtor
WebElement commitBtn = driver.findElement(By.name("commit"));
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOf(commitBtn));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", commitBtn );

Categories

Resources