How to click the button via Selenium with 'onclick' info only? - python

The only thing html has with the button is:
<button onclick="exportTableToCSV('hahaha.csv')">export as csv</button>
No class or id or name or type.
Tried with xpath and link text, neither works.
browser.find_element_by_xpath("//button[onclick=\"exportTableToCSV('hahaha.csv')\"]").click()
browser.find_element_by_link_text("export as csv").click()
Both return to error:
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button[onclick="exportTableToCSV('hahaha.csv')"]"}
Message: no such element: Unable to locate element: {"method":"link text","selector":"export as csv"}
Help appreciated!!!

You can locate this element with XPath or CSS Selector.
The locator you tried has some syntax error.
Please try this XPath:
browser.find_element_by_xpath("//button[#onclick=\"exportTableToCSV('hahaha.csv')\"]").click()
Or this CSS Selector
browser.find_element_by_css_selector("button[onclick=\"exportTableToCSV('hahaha.csv')\"]").click()
In case exportTableToCSV is an unique value you can also use this XPath
browser.find_element_by_xpath("//button[contains(#onclick,'exportTableToCSV')]").click()

The text export as csv is the text of the <button> element but not of an <a> tag. Hence you can't use find_element_by_link_text() to locate the desired element.
Solution
To click() on a element with text as export as csv 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(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[onclick^='exportTableToCSV']"))).click()
Using XPATH:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[starts-with(#onclick, 'exportTableToCSV') and text()='export as csv']"))).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

How to click an element that contains visible text and is clickable with Selenium with Python

The element to be clicked based ONLY on the text it contains (others):
<a class="blah" href="/some_page/"><span>15</span> others</a>
The item that fails:
driver.find_element(By.XPATH, "//*[contains(text(), ' others')]").click()
The error:
NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[contains(text(), 'others')]"}
An additional alternative from DebanjanB's answer using a different xpath inspired from XPath contains(text(),'some string') doesn't work when used with node with more than one Text subnode
"//*[text()[contains(.,' others')]]"
To locate the clickable 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.blah[href='/some_page/'] > span"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='blah' and #href='/some_page/'][contains(., 'others')]"))).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

Selenium chromedriver cannot click href link

I am writing a script to automate data collection and was having trouble clicking a link. The website is behind a login, but I navigated that successfully. I ran into problems when trying to navigate to the download page. This is in python using chrome webdriver.
I have tried using:
find_element_by_partial_link_text('stuff').click()
find_element_by_xpath('stuff').click()
#and a few others
I get iterations of following message when I try a few of the selector statements.
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"partial link text","selector":"download"}
(Session info: chrome=88.0.4324.182)
Html source I'm trying to use is:
<a routerlink="/download" title="Download" href="/itron-mvweb/download"><i class="fa fa-lg fa-fw fa-download"></i><span class="menu-item-parent">Download</span></a>
Thank you!
This is caused by a typo. Download is case-sensitive, make sure you capitalize the D!
To click on the element with text as Download you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element(By.CSS_SELECTOR, "a[title='Download'][href='/itron-mvweb/download'] span.menu-item-parent").click()
Using xpath:
driver.find_element(By.XPATH, "//a[#title='Download' and #href='/itron-mvweb/download']//span[#class='menu-item-parent' and text()='Download']").click()
Ideally, 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[title='Download'][href='/itron-mvweb/download'] span.menu-item-parent"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#title='Download' and #href='/itron-mvweb/download']//span[#class='menu-item-parent' and text()='Download']"))).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

How do I click on this check box using selenium

I would like to select this checkbox using selenium
<input type="checkbox" name="rg" onchange="onCheckReg(this)" id="beauty_perfume_screening__c" value="beauty_perfume_screening__c" tabindex="-1" aria-labelledby="check-button-Beauty Perfume Screening check-group-header">
I tried finding by xpath an by id but always get the error unable to locate element
wd.find_element_by_id("beauty_perfume_screening__c").click()
Any Ideas?
Use xpath
driver.find_element_by_xpath("//input[#name='rg']").click()
and recommended is to use one of the wait methods -
WebDriverWait(driver, 10).until(
EC.presence_of_element_located(By.XPATH, "//input[#name='rg']")
For selecting iframe -
root1 = driver.find_element_by_xpath("//iframe")
driver.switch_to.frame(root1)
To click on the element with text as save you can use either of the following Locator Strategies:
Using id:
wd.find_element_by_id("beauty_perfume_screening__c").click()
Using css_selector:
wd.find_element_by_css_selector("input#beauty_perfume_screening__c[name='rg'][value='beauty_perfume_screening__c']").click()
Using xpath:
wd.find_element_by_xpath("//input[#id='beauty_perfume_screening__c' and #name='rg'][#value='beauty_perfume_screening__c']").click()
Ideally, 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 ID:
WebDriverWait(wd, 20).until(EC.element_to_be_clickable((By.ID, "beauty_perfume_screening__c"))).click()
Using CSS_SELECTOR:
WebDriverWait(wd, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#beauty_perfume_screening__c[name='rg'][value='beauty_perfume_screening__c']"))).click()
Using XPATH:
WebDriverWait(wd, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='beauty_perfume_screening__c' and #name='rg'][#value='beauty_perfume_screening__c']"))).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
Important: Incase the element is within an <iframe> you have to switch to the <iframe> first.
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python
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

NoSuchElementException: no such element: Unable to locate element: css selector but im using find_element

I used selenium IDE to trace my UI activity. I got this following code from IDE and i inspected in UI also,but while using find_element by id i'm getting css selector error.
driver.find_element(By.ID, "button-1034-btnIconEl").click()
error is
raise exception_class(message, screen, stacktrace)
NoSuchElementException: no such element: Unable to locate element:
{"method":"css selector","selector":"[id="button-1034-btnIconEl"]"}
(Session info: chrome=78.0.3904.108)
Please help me to debug this..
To click() on the button with text as Add Order you have to induce WebDriverWait for the element_to_be_clickable() 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[id^='button-'] > span.x-btn-wrap > span.x-btn-button > span.x-btn-inner.x-btn-inner-center"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(#id, 'button-')]/span[#class='x-btn-wrap']/span[#class='x-btn-button']/span[#class='x-btn-inner x-btn-inner-center' and contains(., '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
Reference
You can find a detailed discussion on NoSuchElementException: no such element in Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome
The id seems to be dynamic one so you cannot use static id in the selector. You need to use a dynamic xpath for this.
You can use the below xpath:
driver.find_element(By.XPATH, "//span[contains(#id,'btnIconEl')]").click()
OR
You can find the element using its text as well in the xpath:
driver.find_element(By.XPATH, "//span[contains(text(),'Add Order')]").click()
Might not be your case, but I made a silly mistake that lead me to this error.
I forgot to get the actual web page using the line:
driver.get(my_link)
and thus, I got the same error

Categories

Resources