-1

Im using java programming. Now, here web code;

<button class="btn-link" data-sugg-sources="full_name" data-sugg-technik="make_initial_and_name">NG1ulkan</button> 

if i right click and copy > css selector, web is giving me this code;

'.suggestions > ul:nth-child(2) > li:nth-child(1) > button:nth-child(1)'

Now question is : How can i find this element with this selector and how can i focus and click with selenium java? and also im using JavascriptExecutor js = (JavascriptExecutor) driver; in my code for focus

agean12
  • 1
  • 2
  • could you not find the element by its `class` ? – dumbPotato21 Apr 18 '17 at 16:31
  • no, there are 5 different buttons, so i must select according to this selector '.suggestions > ul:nth-child(2) > li:nth-child(1) > button:nth-child(1)' – agean12 Apr 18 '17 at 16:34
  • 1
    Welcome to Stack Overflow! Please read [ask] and [How much research effort is expected?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) Please provide the code you have tried and the execution result including any error messages, etc. – JeffC Apr 18 '17 at 17:23

2 Answers2

1

.suggestions > ul:nth-child(2) > li:nth-child(1) > button:nth-child(1) is nothing but cssSelector; it just like xpath to locate elements present on a webpage, only that it is based upon css values

As you stated you are using JavascriptExecutor so you can use following code to click this element:

  js.executeScript("document.querySelector('.suggestions > ul:nth-child(2) > li:nth-child(1) > button:nth-child(1)').click();");
Kushal Bhalaik
  • 3,349
  • 5
  • 23
  • 46
0

In Selenium's Java API you're going to use WebDriver.findElement and By.cssSelector:

String selector = ".suggestions > ul:nth-child(2) > li:nth-child(1) > button:nth-child(1)";
WebElement button = driver.findElement(By.cssSelector(selector));

See also this StackOverflow answer.

Community
  • 1
  • 1
Brad Buchanan
  • 1,475
  • 1
  • 15
  • 22