2

I am trying to use Selenium WebDriver to click a button that has the following HTML code:

<a class="uir-item-edit" href="/app/accounting/transactions/transaction.nl?id=400000&amp;e=T" 
id="edit_/app/accounting/transactions/transaction.nl?id=400000" 
aria-label="Edit Sales Order:300000 / 400000 ">Edit</a>

My code:

driver.findElement(By.cssSelector("a[aria-label *= 'Edit Sales Order']")).click();

I need to identify by the Aria Label as all the other components either have duplicates or change w/ the number.

There must be something wrong with my CSS selector, but I can't figure it out.

Let me know if I should include the error codes, more info, or if I did something incorrect w/ the post. Thanks!

Edit: the aria label also changes and the location on the HTML also changes so I can't use xpath. DebanJanB's answer below clicks the element, but only when I manually hover over.

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.uir-item-edit[aria-label^='Edit Sales Order'][id*='transactions']"))).click();
Aaron
  • 33
  • 1
  • 5

4 Answers4

0

You can use class="uir-item-edit". In CSS selector you have to use whole value like: Edit Sales Order:300000 / 400000. Try this

  • Let me know if this Correct: driver.findElement(By.cssSelector("a[aria-label *= 'Edit Sales Order:300000 / 400000 ']")).click(); – Aazad R khan Sep 20 '19 at 05:28
0

I can give you work around, I know you are looking answer for CSS but you can also try below xpath solution:

driver.findElement(By.xpath("//*[@class='uir-item-edit']")).click();

driver.findElement(By.xpath("//*[@class='uir-item-edit'][@aria-label]")).click();

driver.findElement(By.xpath("//*[@aria-label='Edit Sales Order:300000 / 400000']")).click();

driver.findElement(By.xpath("//*[contains(text(),'Edit')]")).click();

Yes, please share more info of error , good luck

Mike ASP
  • 1,103
  • 1
  • 15
  • 18
0

To click() on the element with text as Edit you need to use elementToBeClickable() and you can use either of the following Locator Strategies:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.uir-item-edit[aria-label^='Edit Sales Order'][id*='transactions']"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='uir-item-edit' and starts-with(@aria-label, 'Edit Sales Order')][contains(@id, 'transactions') and text()='Edit']"))).click();
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
0

Remove whitespaces from your CSS selector before * and after = so that instead of a[aria-label *= 'Edit Sales Order'] you would have a[aria-label*='Edit Sales Order']

Reproduced, fixed and testsd in http://try.jsoup.org/~x6PCZNOcp6NnwKPFJN-IdR5gkJk

Alexey R.
  • 4,490
  • 1
  • 7
  • 22