0

Well I have below code

<button class="jobs-search-box__submit-button artdeco-button artdeco-button--3 ml2" data-ember-action="" data-ember-action-689="689">Search</button>

I want to find this element in selenium and perform click action. I tried several options like by class, xpath, name, text, contains but nothing worked. Can someone guide me here?

driver.findElement(By.xpath("//button[contains(.,'Search']")).click();
driver.findElement(By.className("jobs-search-box__submit-button artdeco-button artdeco-button--3 ml2")).click();
driver.findElement(By.className("//*[@id=\"ember689\"]/button")).click();
driver.findElement(By.linkText("Search")).click();
adesh
  • 832
  • 4
  • 14
  • 23
  • How does it not work? Does it say the button is not found? I presume you see it on screen? Because the xpath seems right according to https://sqa.stackexchange.com/questions/2696/selenium-how-to-identify-the-button-webelement. I don't see an id or a link, so I would not expect the last two to work. – Jeremy Kahan Jul 11 '19 at 03:13
  • and by class name seems not to like three classes, see https://github.com/seleniumhq/selenium/issues/1480 – Jeremy Kahan Jul 11 '19 at 03:13
  • Have you checked if the xpath `//button[contains(.,'Search']` is pointing to the right element in the [chrome dev tools](https://stackoverflow.com/questions/55870609/is-there-a-way-to-learn-xpath-without-using-firebug-or-xpath-as-firefox-is-not-s/55870909#55870909)? – supputuri Jul 11 '19 at 03:16
  • 1
    By.cssSelector(".jobs-search-box__submit-button.artdeco-button.artdeco-button--3.ml2") according to https://github.com/seleniumhq/selenium/issues/1480 – Jeremy Kahan Jul 11 '19 at 03:18
  • @supputuri well //button[contains(.,'Search'] this does not point to any element. Am I missing anything here? – adesh Jul 11 '19 at 03:25
  • a closing right parenthesis? so //button[contains(.,'Search')] – Jeremy Kahan Jul 11 '19 at 03:36
  • @JeremyKahan: Yes it turned out to be missing right parenthesis. :| – adesh Jul 11 '19 at 04:08
  • Given that it fixed the problem, is it possible to accept the answer? – Jeremy Kahan Feb 19 '20 at 02:14

4 Answers4

0

To summarize what was in the comments. Each locator had something off.

By.xpath("//button[contains(.,'Search']")

was missing a parenthesis and needed to be:

By.xpath("//button[contains(.,'Search')]")

Meanwhile, because By.className expects a single className

By.className("jobs-search-box__submit-button artdeco-button artdeco-button--3 ml2")

also does not work. (see github.com/seleniumhq/selenium/issues/1480 but could as:

By.cssSelector(".jobs-search-box__submit-button.artdeco-button.artdeco-button--3.ml2") 

Also

By.className("//*[@id=\"ember689\"]/button")

refers to an id not presented (Also, I'm not sure, but I think would need to be by xpath).

By.linkText("Search")

does not work because there is no tag a and so no hyperlink.

In Protractor this is much simpler because you would just say by.buttonText('Search')

Jeremy Kahan
  • 3,796
  • 1
  • 10
  • 23
0

You can achieve the same things by using javascript. kindly find the below example of code:

//Creating the JavascriptExecutor interface object by Typecasting       
JavascriptExecutor js = (JavascriptExecutor)driver;

WebElement button =driver.findElement(By.xpath("//button[@class='jobs-search-box__submit-button artdeco-button artdeco-button--3 ml2']"));

//Perform Click on LOGIN button using JavascriptExecutor        
js.executeScript("arguments[0].click();", button);

I hope it will work on your case.

Note: Make sure your element will be static.

Amit Singh
  • 121
  • 5
0

The correct XPath locator would be:

//button[text()='Search']

If you won't be able to locate it using the above query, make sure that:

  1. The button doesn't belong to and <iframe>, if this is the case - you will have to change the context using switchTo() function
  2. The element is present in DOM, i.e. the page has been loaded fully. It's better to use Explicit Wait for element location/interaction like:

    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Search']")));
    

More information: How to use Selenium to test web applications using AJAX technology

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
0

Try with These two hope it works,

1.) Using Contains

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Search')]")));

2.) Using CSS

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.jobs-search-box__submit-button artdeco-button artdeco-button--3 ml2")));

WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.contains('Search')")));

If does not work let me know i'll provide another Solution.

koushick
  • 497
  • 2
  • 8
  • 30