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:
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();
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();
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();
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')]"))
Related
I am working with Selenium Webdriver with Java.
And I was trying to interact with anchor tag which is enclosed as pseudo element ::before
But I am unable to interact with anchor element.
Here is the screenshot of the HTML structure.
With
JavaScriptExecutor, I understand, we can fetch the propertyValue using window.getComputedStyle().getPropertyValue() but I am not sure, how to interact with <a> element and execute a Click.
Initially, I attempted to click on the Anchor Element without considering the pseudo element as simple Element Interaction.
To fetch the Element:
private By tabRawView_By_CSS = By.cssSelector("[tabid='raw-view'][role='tab']");
But this piece is not throwing any error but it is also not clicking on the element.
Then I thought of using JavaScriptExecutor and was trying to run first in Developers Tool as below image but couldn't find suitable options.
Can anyone please suggest?
If it is Java bindings and all you want to do is to click on an achor tag which has Raw view as a text.
You could try with ExplicitWaits :
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Raw View"))).click();
I am trying to make Selenium click upvote buttons on a reddit-like website. The site has entries from different users and each entry has a upvote and downvote button below it. What i want to do is to make Selenium click on the first upvote button ( which belongs to the entry at the top ) on the page.
I tried to use "Copy XPath" function in Chrome, but all of the upvote buttons on the page return the same XPath:
//*[#id="eksico-chevron-up-thick"]/path
And this is how the website looks like if needed :
So, is there any way for finding the XPath of the first upvote button? I was thinking of something like:
//*[#id="eksico-chevron-up-thick"]/[1]
etc. Thanks in advance.
Edit: The HTML Code of one of the upvote elements:
The element that you are trying to click is under shadow dom as mentioned in the html structure and currently selenium does not support operation on the elements under the shadow dom.
Reference: https://medium.com/rate-engineering/a-guide-to-working-with-shadow-dom-using-selenium-b124992559f
So, if you want to click on the element, you can use JavaScriptExecutor like:
WebElement element = driver.findElement(By.id("eksico-chevron-up-thick"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
By default, it will click on the first element itself and if you want to click on a specific nth element then you can take the elements in a list and then send the index of that element inside the method to get that element clicked.
If you want to click on the first one you could use
Driver.find_elements_by_xpath(//*[#id="eksico-chevron-up-thick"]/path)[1].click()
I want to click on login button. Which attribute should I use to click on login button? I am new to selenium web driver. I am unable to find its link text, id, class name, name. I am not able to find its XPath or CSS selector. please advice me with the code. Image is here
You can use either Xpath which is
.//*[#id='main-menu']/ul/li[8]/a
or CSS Selector which is
.login
To click on element with text as LOGIN within the url https://phptravels.com/404/ you can use either of the Locator Strategies:
CssSelector:
"a.login[href='http://phptravels.org']>span"
XPath:
"//a[#class='login' and #href='http://phptravels.org']/span[contains(.,'LOGIN')]"
//or
"//a[#class='login' and #href='http://phptravels.org']/span[contains(.,'Login')]"
You have multiple options, just avoid using xpath since its an overkill
li.user-login>a.login>span
or
a.login>span
or xpath
.//a[#class='login']/span[text()='Login']
I am using Java and Selenium to write a test. I am going to click on a web element that is covered by another one so I receive the error other element will receive the click. I do NOT want to use Select so how can I click on the web element that is covered?
These are things that I have tried:
action.click(dropdown).build().perform();
action.moveToElement(dropdown).sendKeys(Keys.ENTER).build().perform();
dropdown.click();
Also there is no need to use scroll as the element is on the page but just covered by something else.
You can use JavascriptExecutor
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);
I would not do this as it could miss potential defects. Not sure why you want to perform a click on hidden element.
I am trying to click the image icon via xPath but when i run the code the link present on image icon is not opening. could you please help me in resolving this issue.
The Code i used to click the mail icon :
driver.findElement(By.xpath("//*[#id='e-switcher-mail-icon']")).click();
As slanec said more information is required or might be the element is not loaded. If u feel that the element has loaded and still its not happening, using java script is one way to click the image element.
Something like this
WebElement element = driver.findElement (By.xpath ("//*[#id='e-switcher-mail-icon']"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript ("arguments[0].click();" , element);
Could be that the element hasn't yet loaded in the DOM. Try waiting for expected conditions:
Wait<WebDriver> wait= new FluentWait<WebDriver>(driver).withTimeout(15L, TimeUnit.SECONDS).pollingEvery(1, TimeUnit.SECONDS);
WebElement icon = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[#id='e-switcher-mail-icon']")));
icon.click();
Touché. It's the frames. Either <frame> or <iframe>, both need special care, see the documentation on the topic.
What you need to do:
driver.switchTo().frame("s_MainFrame");
after this, the driver's context will switch to the frame and all searches will be done in it, so you should be able to find the element without any further problems.
Once you're done in the frame and you need to switch back to the default context of the page, do:
driver.switchTo().defaultContent();