1

I need to locate a search bar to search some text listed in the below box and click on it. I tried below code but I couldn't perform the activity.

This code wasn't clicked the search bar:-

driver.findElement(By.xpath("//input[@class='Searchbar__search-field___2FQ0S search-input']")

Image of the HTML: enter image description here

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
User2080
  • 69
  • 4

4 Answers4

1

The desired elements are ReactJS enabled elements within a Modal Dialog so to locate the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following Locator Strategies:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.modal-body#promotion-url-modal-body input.search-input[placeholder='Find a promotion...']"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='modal-body' and @id='promotion-url-modal-body']//input[contains(@class, 'search-input') and @placeholder='Find a promotion...']"))).click();
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • @ debanjanB I tried both codes but its not clicking the search box. I am trying to click the search box and send keys using the items in the screenshot – User2080 Apr 24 '19 at 10:37
  • 1
    @User2080 It is pretty tough to construct an xpath/css from an image. Please read why a [screenshot of HTML or code or error is a bad idea](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). Consider updating the Question with formatted text based relevant HTML, code trials and error stack trace. – DebanjanB Apr 24 '19 at 10:41
  • 1
    @DebabjanB I made some changes(My mistake) and ran with your code, now its working fine. Thanks a lot... – User2080 Apr 24 '19 at 12:13
  • @User2080 [Upvote](https://stackoverflow.com/help/why-vote) the answer if this/any answer is/was helpful to you for the benefit of the future readers. – DebanjanB Apr 24 '19 at 12:18
0

class can be changed, you need to check HTML on failed step. As a workaround you can use more generic xpath: driver.findElement(By.xpath("//input[contains(@class, 'search-input')]"))

0

Do you need to switch to a new frame before doing anything in the modal dialog? Do this:

driver.switchTo().activeElement();

And then try the following CSS locator:

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("#promotional-url-modal-overlay input[placeholder='Find a promotion...']"))).click();
Mate Mrše
  • 6,305
  • 6
  • 26
  • 53
0

Can you provide us with a link to the website you are trying to interact with? Chances are the element is within an iframe, in which case you need to switch to the iframe before you can interact with said element.

Make sure your window is maximised, as this can interfere with iframes:

driver.manage().window().maximize();

Then you will need to find the id of the iframe, using something like firebug

Once you have the id:

driver.switchTo().frame("xxxxxxxxx");

interact with said element within the iframe:

driver.findElement(By.xpath("html/body/a/img")).click();
to240
  • 251
  • 2
  • 4