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.
Related
I'm trying to click this object with selenium (pyhton) using the code:
driver.find_element_by_('publicForm_customFields').click()
But I'm receiving this error:
id="publicForm_customFields" tabindex="0" type="radio" value="value"> is not clickable at point (480, 98). Other element would receive the click: value
"Other element would receive the click" means that you have another element over your element. There are a couple of options to get around this:
Try to find another element above in the DOM tree and click on it.
Use js click, you need to write a function like this:
def js_click(self, element):
element = driver.find_element_by_id("myid")
driver.execute_script("arguments[0].click();", element)
js script will click on the element even if it is intersected by another
There might be two possibilities:
1- Your locator to find element might be wrong or not unique.
2- You need to apply explicit wait till element is ready [load successfully] to be clickable.
Hope above possibilities might help you out, Or you share the link of the sight so I might debug correctly.
You are not passing any find method to the driver. Try refactoring the code to:
driver.find_element_by_id('publicForm_customFields').click()
Also, try using some sort of wait so the driver does not click before the page/element is loaded.
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.ID, 'publicForm_customFields'))).click()
I am fairly new in Selenium and I've been trying to work on automating the login for this [website], but for some reason element.click() on selenium does not seem to work when I try to click onto the Login button. I keep getting this TypeError: 'str' object is not callable error.
Here's my code:
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH("//li[#class='item-119']/a[contains(text(),'Login')]")))
)
print("TEST")
element.click()
finally:
driver.quit()
I've also tried using By.CLASS_NAME and By.PARTIAL_LINK_TEXT and I keep getting the same error. Spent a few hours researching, looking through StackOverFlow and trying to solve this error but I don't seem to be able to solve it. Please do help me and let me know where I went wrong.
You are trying to use wrong locator.
Selenium fails to find such element.
Also, it's better to use expected conditions of element visibility instead of element presence since when element becomes existing on the page it is still not clickable / visible.
So, please try this:
element = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[#id='header']//a[#class='login-btn']")))
print("TEST")
element.click()
You can try with below xpath :
try:
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH("(//a[#class='login-btn'])[2]"))).click()
print("TEST")
finally:
driver.quit()
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
I wanted the program to click the 'PLAY' Button on spotify to play a song, but sadly I get this error
selenium.common.exceptions.JavascriptException: Message: javascript error: arguments[0].click is not a function
The code I tried is
WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.XPATH,"//*[text()='PLAY']")))
time.sleep(1)
driver.execute_script("arguments[0].click();", driver.find_element_by_xpath("//*[text()='PLAY']"))
Can someone help me to find the button and click it, I tried also the
driver.find_element_by_xpath("//*[text()='PLAY']").click()
But I get the error element is not interactable
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(driver.find_element_by_xpath("//div[#class='TrackListHeader__button
TrackListHeader__button--top']//button")));
Thread.sleep(1000);
driver.find_element_by_xpath("//div[#class='TrackListHeader__button
TrackListHeader__button--top']//button").click();
also try this one
EC has a good method for verifying element is clickable before making an action element_to_be_clickable.
Also, I found over 20 elements on Spotify using your XPath, so I modified it to look for button instead of *.
Try this:
WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.XPATH,"//button[text()='PLAY']")))
driver.find_element_by_xpath("//button[text()='PLAY']").click()
I hope this helps. Good luck!
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 :)