How to find XPath of select file button? - java

I need to click on Select file button. Have tried by using following XPath:
driver.findElement(By.xpath("//div[#class='ant-modal-body']//button[contains(#class,'ant-btn-ghost')]/i")).click();
But the above code didn't work for me.
Following is the code which I get after pressing F12:
<button class="ant-btn ant-btn-ghost" type="button">
<i class="anticon anticon-upload"/>
<span> Select File </span>
</button>

As you mentioned in your comments that you solved your issue through Thread.sleep(2000) that would be against all the Best Practices. To to click on the button with text as Select file you have to induce WebDriverWait and you can use the following line of code :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='ant-modal-body']//button[#class='ant-btn ant-btn-ghost']//span[contains(.,'Select File')]"))).click();

First try with :
driver.findElement(By.xpath("//span[contains(text(), 'SelectFile')]")).click();
If it doesn't work try :
WebElement composeBtn = driver.findElement(By.className("ant-btn ant-btn-ghost"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", composeBtn);

Related

Clicking on a button with Selenium using Xpath or CSS Selector

I've spent a couple of hours trying to click on a button using various selectors before asking a question here, but nothing seems to work
WebElement add= driver.findElement(By.partialLinkText("+"));
WebElement add= driver.findElement(By.xpath("/*[name()='svg']/*[name()='button']"));
WebElement confirm = driver.findElement(By.xpath("//a[contains(#class, 'IconButton-h85035-0 indexstyles__PlusButton')]"));
No available examples have a similar html layout as the page I'm working with.
I am not sure how to create the xPath to the button.
I would appreciate any hints on how to create xPath, or the CSS selector, no ready solution is expected, but any help to understand how to refere to that particular element.
That is the code of the plus button:
<button data-testid="tselectionSpinbuttonPlus" type="button" tabindex="-1" aria-hidden="true" width="44px" height="44px" class="IconButton-h85035-0 indexstyles__PlusButton-sc-108enfc-3 bAZDfp">
<svg viewBox="0 0 24 24" width="1.5em" height="1.5em" aria-hidden="true" focusable="false" class="BaseSvg-sc-9y47q5-0 PlusIcon___StyledBaseSvg-sc-11rza9m-0 VCaQT">
<path d="M13 11V3h-2v8H3v2h8v8h2v-8h8v-2h-8z">
</path>
</svg>
</button>
To locate/click on the element you can use either of the following Locator Strategies:
cssSelector:
driver.findElement(By.cssSelector("button[data-testid='tselectionSpinbuttonPlus']")).click();
xpath:
driver.findElement(By.xpath("//button[#data-testid='tselectionSpinbuttonPlus']")).click();
Ideally to locate/click on the element you need 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("button[data-testid='tselectionSpinbuttonPlus']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#data-testid='tselectionSpinbuttonPlus']"))).click();
Pls try with below:
Code snipt
WebElement add= driver.findElement(By.partialLinkText("+"));
Actions ac1 = new Actions(driver);
ac.clickAndHold(add).build().perform();

Selenium Webdriver, clicking a button not working

I am having two issues in clicking two buttons during my automation
First : A View button which have the following Details
<button class="veiw-btn" data-toggle="collapse" data-target=".toggle-content1" aria-expanded="false" aria-controls="toggle-content1" ng-click="gotoAnchor(flightResult.FlightId)">VIEW</button>
There are several VIEW buttons on the page but they are differentiated with the toggle-content (they have numbers 1,2,3,4) I just need to select the first one and click on it
Second :
After clicking the View i want to also click the Continue Button with the following code
<div class="text-center">
<button class="flight-book-btn" type="button" ng-click="select(false, flightResult);">
<span>Continue</span>
</button>
</div>
My Major issue is the First Code but if i can get help with both i will be glad . I have not been able to click on the First VIEW Button
i have tried some samples online but they have not worked for me
I expect to be able to click the VIEW and Continue Buttons
CODE:
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
log.debug("Fastest Sort Available ");
log.debug("Now about to click VIEW Airline Details ");
// driver.findElement(By.xpath("//button[text()='VIEW' and #data-target='.toggle-content1' and #aria-controls='toggle-content1']")).click();;
driver.findElement(By.cssSelector("button[class=\"veiw-btn\"][data-target='.toggle-content1'']")).click();
Try using the css selector or Xpath
CSS:
driver.FindElement(By.CssSelector("button[class="veiw-btn"][data-target='.toggle-content1']").Click();
xpath:
driver.FindElement(By.XPath("//button[#class='veiw-btn'][#data-target='.toggle-content1']").Click();
You can use this xpath to click on VIEW button :
//button[text()='VIEW' and #data-target='.toggle-content1' and #aria-controls='toggle-content1']
For clicking on Continue button you can try with this xpath :
//span[text()='Continue']/parent::button[#class='flight-book-btn']
I have fixed this by using this
WebElement element= driver.findElement(By.xpath("//button[text()='VIEW' and #data-target='.toggle-content1' and #aria-controls='toggle-content1']"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);

Unable to locate the button or link using either xpath, id, name or css selector

Unable to locate the element using id/name/xpath/CSSSelector
Tried the below codes and both failed to give response
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id=\'form\']/p/button/span")));
driver.findElement(By.xpath("//*[#id=\'form\']/p/button/span")).click();
and
WebElement checkout = driver.findElement(By.xpath("//[#id=\'form\']/p/button/span"));
checkout.click();
HTML
<button type="submit" name="processCarrier" class="button btn btn-default standard-checkout button-medium" style="">
<span> Proceed to checkout <i class="icon-chevron-right right"></i> </span>
</button>
Try Following CSS Selector.
WebElement checkout = driver.findElement(By.cssSelector("button.standard-checkout span"));
checkout .click();
Or yon can use WebDriverWait and Css Selector.
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.standard-checkout span")));
element.click()
Probably you are getting org.openqa.selenium.InvalidSelectorException because you should use * after // to match any node(tag) who has id=form or the specific tag name.
change it to //*[#id='form']/p/button/span
Or use more specific like
xpath : //button[#name='processCarrier'] equivalent CSS : button[name='processCarrier']
And use implicit/explicit wait to make element available in DOM to perform actions.
Presumably you will invoke click() on the <button> element, so you need to induce WebDriverWait for the desired element to be clickable and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.standard-checkout.button-medium[name='processCarrier']>span"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='button btn btn-default standard-checkout button-medium' and #name='processCarrier']/span"))).click();

Not able to click a button using Selenium and Java. It has "style=display: none" in html which is causing issue

I have tried with different xpaths etc but not able to click a 'Continue' button in my application. Following is one of the attempt to handle the same:-
WebDriverWait wait2 = new WebDriverWait(driver,20);
WebElement continueBtn = wait2.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#type = 'submit' and #class = 'btn btn--primary']")));
When I inspect element I find follwing details in it: -
<button class="btn btn--primary" type="submit" data-bind="enable: !processing() && !$root.accountLocked()">
<svg xmlns="http://www.w3.org/2000/svg" class="icon shape--loader" style="display: none;" viewBox="0 0 16 16" focusable="false" data-bind="visible: processing()">
<title>Processing</title>
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#shape--loader" />
</svg>
Continue
</button>
The error thrown is as follow:-
Exception in thread "main" org.openqa.selenium.TimeoutException: Timed out after 20 seconds waiting for element to be clickable: By.xpath: //*[#type = 'submit' and #class = 'btn btn--primary']
However the object/element was already loaded so not sure why it couldn't click the element.
I also tried JavaScript executor but to no avail.
Please help me resolve it. Thanks in advance !!!
Just to clear the air, is the XPath given above is unique?
If yes did you give it a try with JSExecutor?
Try with below code:
WebElement element = driver.findElement(By.xpath("//*[#type = 'submit' and #class = 'btn btn--primary']")));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
If it is difficult to findelement with attributes, then find it based on button name. If there is only one Continue button in the page like.
WebElement continueBtn = wait2.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Continue')]")));
Finally I got the solution. Before reaching solution I tried explicit wait, code of which I had pasted in question itself and then tried with JavaScript executor but it also didn't work. Following is the solution:-
Actions action = new Actions(driver);
action.sendKeys(Keys.ENTER).build().perform();
It worked perfactly fine. Thank you guys who all contributed their time and efforts.

I am unable to get xpath for walmart homepage dropdown box using selenium webdriver in Java

I am trying to code in java using selenium webdriver to click on Dropdown list in walmart page. But I am unable to access the li elements.
<button class="js-flyout-toggle dropdown" aria-haspopup="true" type="button" data-cat-id="0" aria-expanded="false"> All </button>
<div class="js-flyout-modal flyout-modal">
<ul class="block-list">
<li><button class="no-margin font-semibold" type="button" data-cat-id="0" tabindex="-1"> All Departments </button></li>
<li><button class="no-margin font-semibold" type="button" data-cat-id="91083" tabindex="-1"> Auto & Tires </button></li>
<li><button class="no-margin font-semibold" type="button" data-cat-id="5427" tabindex="-1"> Baby </button></li>
I wanted to access Baby using selenium webdriver in Java.
Below is my code:
driver.get("http://www.walmart.com");
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement dropdown = driver.findElement(By.xpath("html/body/div[2]/header/div[3]/div/div/div/div/div[3]/form/div/div[1]/div/button"));
dropdown.click();
List<WebElement> liElements = driver.findElements(By.xpath("//*[#class='block-list']/li"));
for ( WebElement we: liElements) {
System.out.println(we.getText());
}
But this gives an error as below:-
"Exception in thread "main"
org.openqa.selenium.ElementNotVisibleException: Element is not
currently visible and so may not be interacted with Command duration
or timeout: 132 milliseconds".
Please help
You defined WebDriverWait but you don't use it
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement dropdown = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("dropdown")));
Implicit wait will wait for element to exist in the DOM. If you want to use explicit wait you need to use Expected Conditions as well. Only defining WebDriverWait doesn't actually do anything.
Your code looks fine. Please try below xpath :-
//div[#class='js-flyout-modal flyout-modal']//ul[#class='block-list']/li
Now first try to put thread.sleep if it works then it is problem of wait only
Thread.sleep(30000);
But then don't use Thread for your script as it's not recommended .. It's just to ensure that script is failing because of time
Hope it will help you :)
May be other element in walmart page match your xpath //*[#class='block-list']/li and that element is not visible.

Categories