Reputation: 69
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:
Upvotes: 1
Views: 400
Reputation: 56
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')]"))
Upvotes: 0
Reputation: 381
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();
Upvotes: 0
Reputation: 8444
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();
Upvotes: 0
Reputation: 193308
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();
Upvotes: 1