1

I'm trying to find a "button" and click it but it has no id. I tried using Xpath and even cssSelector but nothing works.

This are the elements of the button:

div style="cursor: grabbing;" title="Approved Forms" onclick="goToForms(0)" class="col-lg-2 col-sm-6"

I tried this :

driver.findElement(By.xpath("//div[contains(@title=,'Approved Forms')]")).click();
driver.findElement(By.xpath("/html/body/div[3]/div/div/div[2]/div[1]")).click(); *copy xpath*
driver.findElement(By.cssSelector(['title="Approved Forms"'])).click(); *has a sintax errors, not sure how to write it*
TheDude
  • 13
  • 3
  • You need to validate XPath and CSS before including them into your test scripts. You can use chrome developer tool to verify the CSS and XPath. [For reading more about XPaths]( http://pragmatictestlabs.com/2020/01/28/mastering-xpath-for-selenium-test-automation-engineers) . [Free course on Location strategies](https://testautomationu.applitools.com/web-element-locator-strategies/chapter1.html) – Janesh Kodikara Jan 12 '21 at 13:23

1 Answers1

0

To locate the element you can use either of the following Locator Strategies:

  • cssSelector:

    WebElement element = driver.findElement(By.cssSelector("div[title='Approved Forms'][onclick^='goToForms']"));
    
  • xpath:

    WebElement element = driver.findElement(By.xpath("//div[@title='Approved Forms' and starts-with(@onclick, 'goToForms')]"));
    

Ideally, you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:

  • cssSelector:

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[title='Approved Forms'][onclick^='goToForms']")));
    
  • xpath:

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@title='Approved Forms' and starts-with(@onclick, 'goToForms')]")));
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352