0
votes

I'm using selenium and python for automated test and have an issue when try to click on an element in web.

I'm using find_element_by_xpath, provide the correct path (I have try on browser and it return 1 of 1).

driver = webdriver.Chrome()
driver.get('page_url')
driver.find_element_by_xpath(//button[@class="aoc-end md-button md-ink-ripple"])

Here is html:

<div class="_md md-open-menu-container md-whiteframe-z2 md-active md-clickable" id="menu_container_77" style="top: 499px; left: 866px; transform-origin: right top;" aria-hidden="false">
 <md-menu-content class="agent-dropdown-menu-content new-menu-content" width="3" role="menu"><!----><md-menu-item>
    <button class="aoc-end-work md-button md-ink-ripple" type="button" ng-transclude="" ng-disabled="!agent.capabilities.canSupervisorLogout" ng-click="logOutAgent(agent.userHandle)" role="menuitem">

The element should be found but actual result is that selenium.common.exceptions.ElementNotVisibleException: Message: element not visible

3
Do you any kind of scroll down or up manually to see that element ? - cruisepandey
The element is visible when open drop-list and don't need to scroll to see it - An Khang
Can you share the HTML code for drop down ? - cruisepandey
My guess is that there is more than one element that matches that locator and the first available is not visible. Check your locator and update the question. Please post valid/complete HTML. The HTML you've got right now isn't complete and isn't formatted in easily readable form. - JeffC

3 Answers

0
votes

From the exception, it seems the element is present of the page but currently not visible. Possible reason for invisibility of element can be (element is masked by another element, element can be in a drop - down list etc. ). If element is in drop-down list, then first open the list and then find the element.

Hope it will help.

0
votes

Finding locators in web application developed in angular is quite tricky, especially when the developers don't follow any of guidelines useful from an automation perspective, like adding unique attributes for every web elements and etc...

In your case, it seems that the same thing is happening.

Following locator should work for you (by the way, you missed "" after and before locator in driver.find_element_by_xpath() method):

driver.find_element_by_xpath("//button[@ng-click='logOutAgent(agent.userHandle)']");
0
votes

Since the element you are trying to click is a dynamic element, you will need to wait for it to become clickable before clicking on it. Use Expected conditions and WebDriver wait:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//button[@class='aoc-end md-button md-ink-ripple']"))).click()

Also, notice the " " and ' ' quotes on the outer and inner xpath selector.