Home > Blockchain >  Having issues finding an element to serach by to click on with Selenium
Having issues finding an element to serach by to click on with Selenium

Time:02-03

Hi I am trying to click on a button which has the following html structure that I found from chrome inspect.

<div >
    <button type="submit" tabindex="3" data-ng-click="login()"  data-ng-disabled="loginForm.$invalid" data-ng->Login</button>
</div>

I am not sure what to find the element by. ID, Name, XPath, etc.

CodePudding user response:

I would suggest using the following XPath here:

//button[@data-ng-click="login()"]

But still need to validate this gives an unique locator

CodePudding user response:

The desired element is a Angular element element, 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 and CSS_SELECTOR:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.btn.btn-default[data-ng-click='login()'][data-ng-class*='loginForm']"))).click();
    
  • Using and XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-default' and @data-ng-click='login()'][text()='Login']"))).click()
    
  • Note: Using Python clients 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
    
  •  Tags:  
  • Related