Clicking on a non-visible button is Selenium - python

I'm trying to click on a button that is only visible when hovering over it.
move_to_element doesn't seem to work and I get this error when trying to click ElementNotVisibleException: Message: element not visible
My code is:
full_screen_elem = driver.find_element_by_xpath(
'//*[#id="grid"]/div[2]/div[1]/article/div[3]/a')
hover = ActionChains(driver).move_to_element(full_screen_elem)
hover.perform()
full_screen_elem.click()
I've also tried this:
driver.execute_script('arguments[0].click();', full_screen_elem)
But it also doesn't work.
What else can I try?

Try and make it visible using Javascript.
I assume the style-attribute of your element is set to hidden. You must set this to visible.
You can do this as following:
driver.execute_script('document.getElementById("element").style.visibility = "visible";);
Then you can interact with the element.
Be sure to double check the syntax from above script because I am a little rusty :)

Related

Button/Element not interactable

I am trying to click on a button in a webpage using Selenium and xpath. I managed to click on a button in the previous page but after loading into this new page, I tried to use the same code but different button HTML, it was not able to load as before.
My code is as follows:
driver = webdriver.Chrome()
driver.get("https://connect.com")
driver.find_element_by_xpath("//button[#class='p-button p-component']").click() #button workable
In the next page:
driver.find_element_by_xpath("//button[#class='add icon-only proto-button ng-star-inserted']").click() #button not working
The error message that I have received is:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
I have also tried with adding in waiting time as below, but it is still not working.
button = WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.XPATH, "//button[#class='add icon-only proto-button ng-star-inserted']")))
button.click()
However, now the error message that I have received is:
selenium.common.exceptions.TimeoutException: Message:
Alternatively, I have tried to work on other elements of the HTML code, for example. But either way, the button did not work.
driver.find_element_by_xpath("//span[text()='proto-icon ng-star-inserted']").click()
or
driver.find_element_by_xpath("//connect-button[#class='proto-button-list-item ng-star-inserted']").click()
The HTML is in the picture attached
I have no idea how else can i try to work around. Please advise, thanks !
I agree with #lucasnguyen17
The button element must be not clickable because of the connect-button element over it.
So
element = driver.find_element_by_xpath("//button[#class='p-button p-component']")
driver.execute_script("arguments[0].click();", element)
It's really simple,
In the first line it stores the element as a python variable.
Then it runs the js script.
element.click();
Where the element is your specific button. You can try it on the console to test if it works.
A link to the specific page would have been helpful.

How to hover over and expand dropdown menu on komoot with selenium?

My goal is to get an element within a dropdown menu that needs to be clicked in order to appear using python selenium. Also, it seems that before the click, hovering is necessary.
I tried to click the dropdown menu like this:
element = wait().until(EC.presence_of_element_located((By.CLASS_NAME, "c-topmenu c-topmenu--create c-topmenu--userbar tw-inline-flex")))
element.click()
def wait():
return WebDriverWait(driver, 30)
And tried to use hovering:
Hover = ActionChains(driver).move_to_element(element)
Hover.click().build().perform()
Still, I get a TimeoutException and can't figure out a solution.
The website komoot looks like this before clicking the dropdown menu:
The website komoot looks like this after expanding it:
Ok here is how i was able to get this working.
after the login i grapped the wrapper element from the link with:
more_menu_wrapper = driver.find_element_by_css_selector("div[data-test-id='more_menu']")
after that i could click the desired element with:
more_menu_wrapper.find_element_by_tag_name("a").click()
You are using a wrong locator.
In case of multiple class names you should use css_selector or XPath, not by class name.
Also, here you have an unique attribute of data-test-id, you should use it.
Also, you should wait for element visibility or clickability, not just for element presence.
So your code could be:
element = wait().until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[data-test-id='more_menu']")))

What is wrong with my xpath? Why is a different element receiving the click?

Steps to recreate:
Go to http://practice.automationtesting.in/
Click on add to basket
Then view basket
Change the quantity to 2
Click on Update Basket
Click on remove item'X'
Or use coupon code "krishnasakinala"
Click on Apply Coupon button
The error I am getting is:
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element ... is not clickable at point (216, 220). Other element would receive the click:
I have tried different locators but getting the same error.
Read more: https://softwaretestingboard.com/q2a/4041/exceptions-elementclickinterceptedexception-intercepted#ixzz6iUxRC97y
My code:
url = "http://practice.automationtesting.in"
driver.get(url)
wait_timeout = 5
wait_variable = W(driver, wait_timeout)
driver.find_element_by_xpath("//a[#data-product_id='160']").click()
wait_variable.until(E.presence_of_element_located((By.XPATH, "//a[#class ='added_to_cart wc-forward']"))).click()
wait_variable.until(E.presence_of_element_located((By.XPATH, "//input[#type='number']"))).clear()
wait_variable.until(E.presence_of_element_located((By.XPATH, "//input[#type='number']"))).send_keys(2)
wait_variable.until(E.element_to_be_clickable((By.NAME, "update_cart"))).click()
wait_variable.until(E.presence_of_element_located((By.XPATH, "//a[#title='Remove this item']"))).click()
There are multiple ways to solve this problem like making the driver wait, using a different element identifier (CSS/class/etc.) but the most effective one is to use the method execute_script()
element = driver.find_element_by_xpath('the xpath of the element')
driver.execute_script("arguments[0].click();", element)
wait_variable.until(E.element_to_be_clickable(
(By.NAME, "update_cart"))).click()
wait_variable.until(E.presence_of_element_located(
(By.CSS_SELECTOR,'[class="blockUI blockOverlay"]')))
wait_variable.until(E.staleness_of(
driver.find_element_by_css_selector('[class="blockUI blockOverlay"]')))
wait_variable.until(E.presence_of_element_located(
(By.XPATH, "//a[#title='Remove this item']"))).click()
The error is due to the overlay loader use this code , using javascript to click an element is not a right way it will bypass the UI rendering and direcly trigger click event on the element.
This should be avoided if you are using selenium for testing
How to find locator for overlay loader:
what is overlay loader :
This spinner is a UI element used to block user interaction with UI while ajax components are being loaded in the background.
What are the challenges:?
The spinner load so fast that it is hard to locate the element from dev tool
How to find the locator?
open element tab , right click the html and seelct break on sub tree modification
now click play button:
click update cart and click the blue play button till the loader is present in the UI.
This will freeze UI with the spiner in screen , now you can easily find the locator

Why a click function of a web page element doesn't work (using Selenium module of Python)?

I am trying to automate a task which can save me thousands of clicks.
I searched a bit for available modules in Python and I have selected to work with Selenium.
I installed Firefox drivers and did some progress but I am stuck for a long time. I finally gave up, opened a Stack Overflow account and wanted to bring this problem into this helpful medium.
My code can successfully do some clicks, but I could not make the code click a button like element. I have to click such items so that page opens some new elements. (Hopefully I am going to click on these new elements to save some excels files automatically).
Here is the part which works as expected:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
browser = webdriver.Firefox()
url = "https://www.tbb.org.tr/tr/bankacilik/banka-ve-sektor-bilgileri/istatistiki-raporlar/59"
browser.get(url)
time.sleep(2)
bank_reports_element_xpath = "//*[#title= 'Tüm Raporlar']"
bank_reports_element = browser.find_element_by_xpath(bank_reports_element_xpath)
bank_reports_element.click()
time.sleep(2)
second_item = "//div[#data-catid = '52']"
finance_tables_element = browser.find_element_by_xpath(second_item)
finance_tables_element.click()
years_item = "//div[#class = 'years']"
years_element = finance_tables_element.find_element_by_xpath(years_item)
year_elements = years_element.find_elements_by_xpath("a")
There I would like to click on the years.
a screenshot of the years that I can't click using Selenium
As an example, I get the element related to year 2004.
year2004 = year_elements[2]
Issuing a year2004.click() command gives an ElementNotInteractableException exception.
year2004.click() # ElementNotInteractableException: Element could not be scrolled into view
Based on searching similar posts, I tried the following (executing the click via javascript). I got no error but it does not do anything. When I click the year2004 button with mouse, a list pops-up in the page. But when I run the below code, no list pops up in the page.
browser.implicitly_wait(4)
browser.execute_script("arguments[0].scrollIntoView();", year2004)
browser.execute_script("arguments[0].click()", year2004)
I tried also the following code. I get "TypeError: rect is undefined"
browser.implicitly_wait(4)
action = ActionChains(browser)
action.move_to_element(year2004) # gives: TypeError: rect is undefined
action.click(on_element = year2004) # gives: TypeError: rect is undefined
action.perform()
I tried using Chrome Driver and got similar results. The errors definitions were not exactly the same. Instead of the above "rect is undefined" error, Chrome error is like: "Javascript error: Failed to execute 'elementsFromPoints' on 'Document': Provided double value is non-finite"
I don't know if it is relevant but year2004.location dictionary has a 0 value for "x".
I would appreciate any answer,
Keep safe
Use this xpath will give you all the elements of year in 52 category
//div[#data-catid='52']/descendant-or-self::*/div[#class='years']/child::a
for scrolling try
browser.send_keys(Keys.PAGE_DOWN)
I checked manually and page down button is working for me press it twice for desire element

Using Python and Selenium I am unable to get to elements in overlaying model

In my test I have to click a button to add an item to the cart. At that point a model dialog comes up and allows some options and links, one of which I am trying to click; View Cart and Checkout.
Every attempt I make to find my element with find_element_by fails with a NoSuchElementException. Doesn't matter how I try to find it; ID, Class, Link Text, etc.
I have tried setting up WebDriverWait in a try/catch statement to see if pausing for the model to be added to the page would help, and it doesn't. I have also checked to see if there is another window or frame to switch to, but there is only the one window.
Are there any other ways to get what I need done, or troubleshooting steps that I can do to figure this out?
Below is what Firefox shows the HTML to be.
<div class="summary">
<dl></dl>
<a class="bb-button" href="/shopping/cart">View Cart and Checkout</a>
<div class="continue"></div>
The python I have written is:
product_details.addDocumentToCart('Printed Edition')
try:
WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(By.CLASS_NAME, 'bb-button'))
print 'bb-button found'
except:
print 'bb-button not found'
windows = self.driver.window_handles
print 'Windows after: ' + str(len(windows))
element = self.driver.find_element_by_class_name('bb-button')
element.click()
The messages I printed out return:
added_to_cart-container not found
and
Windows after: 1
The error I get when running this is:
NoSuchElementException: Message: Unable to locate element: {"method":"class name","selector":"bb-button"}
Thanks Mikko, with your advice about the pbd.set_trace() I was able to discover that my problem was really with the button that I thought was being clicked but wasn't. Further troubleshooting showed that my addDocumentToCart() method was clicking the wrong element, and so my model didn't come up, but no error was generated which lead me to believe that things were working when the really were not.

Categories

Resources