Basically I need to click on a button, which is inside a frame on a webpage. I have tried:
1) Switching to frame, which works fine, doesn't return any errors:
driver.switch_to.frame(driver.find_element_by_css_selector('#iframe'))
3) Adding time delay of 20 seconds, which doesn't change the result as it just times out in the end:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "dx-button"))).click()
I believe the CSS element's name is correct as I have copied it using Developer Mode -> Copy Selector.
Is there anything else that I can do in order for selenium to find this CSS element?
Error before adding time delay:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"dx-button"}
Error after adding time delay:
selenium.common.exceptions.TimeoutException: Message:
Thank you.
May be check if the switched iframe is the accurate one that has the button.
If that's correct, Can you try the same line with XPATH locators?
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[contains(#class, 'button')]"))).click()
Related
I am trying to retrieve the text of a specific element in a table with the following XPATH:
/html/body/form[2]/table/tbody/tr/td/table/tbody/tr[2]/td[7]/input
using
driver.maximize_window() # For maximizing window
driver.implicitly_wait(3) # gives an implicit wait for 20 seconds
driver.find_element(By.XPATH, value = "/html/body/form[2]/table/tbody/tr/td/table/tbody/tr[2]/td[7]/input").text()
but I get the following error:
NoSuchElementException: Message: Unable to locate element: /html/body/form[2]/table/tbody/tr/td/table/tbody/tr[2]/td[7]/input
I have also tried accessing the element by CSS selector and value, without success.
Unfortunately the link is secured so I cannot share it but here is a screenshot of the element
Instead of your absolute XPath, try relative XPath. Below is the expression:
//input[#name='AdjIncrementAmount_1']
from the screenshot, I am not really sure if the value of attribute name (after the text Amount) has one _ or two __. If the above with(one _) doesn't work, try with 2 as below:
//input[#name='AdjIncrementAmount__1']
To print the text $36,400.00 you can use either of the following locator strategies:
Using css_selector:
print(driver.find_element(By.CSS_SELECTOR, "td > input[name='AdjIncrementAmount__1']").get_attribute("value"))
Using xpath:
print(driver.find_element(By.XPATH, "//td/input[#name='AdjIncrementAmount__1']").get_attribute("value"))
Note : You have to add the following imports :
from selenium.webdriver.common.by import By
I want to use selenium to find a div:
My code is :
self.browser.find_element_by_xpath('//div[#class="bh-headerBar-nav-item"]').click()
But I got the error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class="bh-headerBar-nav-item"]"}
What should I do to find this div?
You can try changing the style value of the element to block using javascript.
self.driver.execute_script("document.getElementsByClassName("class-name")[0].style.display = 'block';")
Then try to access the element.
Short:
Your xpath looks correct for the exampled page html, but the class on screenshot ends with the space char. (it could be the reason).
Longer:
If you getting NoSuchElementException there can be 3 cases:
your div present in iframe, so you need to switch to iframe with driver.switch_to.iframe command before invoke find_element.
you looking for div with the exact class match. class attribute sometimes changed after some actions, some classes could be added or removed. So, try to use //div[contains(#class, 'your_class')]. I see, your class ends with the space char on screenshot, it may affect.
the element really not present on the page.
Try to take screenshot, or print pagesource and look for the element, may be this will bring more information regarding the real reason.
The DIV tag is a descendant of the A tag.
To click on the descendant DIV you can use either of the following Locator Strategies:
Using css_selector:
self.browser.find_element(By.CSS_SELECTOR, "a[href='#/sportVenueBookBC'] div.bh-headerBar-nav-item.").click()
Using xpath:
self.browser.find_element(By.XPATH, "//a[#href='#/sportVenueBookBC']//div[#class='bh-headerBar-nav-item ']").click()
However, 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(self.driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href='#/sportVenueBookBC'] div.bh-headerBar-nav-item."))).click()
Using XPATH:
WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#href='#/sportVenueBookBC']//div[#class='bh-headerBar-nav-item ']"))).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
There are two "EXPORT" buttons on the web page I am testing.
I am able to click the first using:
driver.find_element_by_xpath("//button[contains(.//text(),'EXPORT')]").click()
the driver then choses a dropdown and another div appears, again with an EXPORT button.
My code error when trying to execute:
driver.find_element_by_xpath("//button[contains(.//text(),'EXPORT')]").click()
for this element is:
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element ... is not clickable at point (1824, 89). Other element would receive the click: ...
(Session info: chrome=92.0.4515.107)
So it says it is unable to click the first EXPORT button and the second one would get the click.
I've tried modifying my xpath expression to:
driver.find_element_by_xpath("//button[class()='MuiButtonBase-root MuiButton-root MuiButton-contained MuiButton-containedPrimary' and (.//text(),'EXPORT')]").click()
and a bunch of other attempts, but each time it is unable to locate. I want to be able to click the second EXPORT button (in the button class snippet shown in the pic).
You can differentiate between these two buttons with the help of xpath indexing.
Sample below :-
(//span[text()='EXPORT']/..)[1]
and for second button :-
(//span[text()='EXPORT']/..)[2]
and for this error
selenium.common.exceptions.ElementClickInterceptedException: Message:
element click intercepted: Element ... is not clickable at point
(1824, 89). Other element would receive the click: ... (Session info:
chrome=92.0.4515.107)
I would suggest you to open browser in full screen mode when it's launched.
driver.maximize_window()
or probably, you can use ActionChain as well :-
from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).move_to_element(driver.find_element(By.XPATH, "(//span[text()='EXPORT']/..)[2]")).click().perform()
Update 1 :-
You can use find_elements as well to store all the web element and then try to click the one you want.
all_export_button = driver.find_elements(By.XPATH, "//span[text()='EXPORT']/..")
#to click on first
all_export_button[0].click()
#to click on Second
all_export_button[1].click()
error: raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: chrome=86.0.4240.75)
my code: driver.find_element_by_xpath('//button[contains(text(),"Buy Now")]').click()
html: https://i.stack.imgur.com/h0wGd.png
Since we do not have the full HTML page to find an accurate xpath and we have a small screenshot of the code, you could try to find the element using a different xpath technique.
//button[#class='button bits' and contains(., 'Buy Now')]
Then, you could get the element and click on it by using one of the following options. Option 1
driver.find_element(By.XPATH, "//button[#class='button bits' and contains(., 'Buy Now')]").click()
Option 2
driver.find_element(By.XPATH, "//button[#class='button bits' and contains(text(), 'Buy Now')]").click()
According to selenium documentation, that error occurs when:
"Thrown when an element is present in the DOM but interactions
with that element will hit another element do to paint order"
Make sure that your element is unique identified and use a wait. If is not enough, perform the click action via JavaScript.
A rather 'tricky' question that makes my head spin. I'm unable to click a button while using Selenium.
Css selector property: ValidateOrderForm > span > button
Xpath: #//*[#id="ValidateOrderForm"]/span/button
Attempts:
d.find_elements_by_xpath('ValidateOrderForm')[1].clear()
d.find_elements_by_id('ValidateOrderForm')[0].click()
d.find_element_by_css_selector("button.call-to-action").click()
And really everything in between.
Update: it gives the following error
ElementNotVisibleException: element not interactable