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 !
CodePudding user response:
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.