Clicking buttons with Selenium WebDriver Java - java

There is a button on a website I am testing and Selenium can't seem to click it. I have tried many different ways but still nothing. Attached is the HTML for the button as well as the XPath.
Has anyone else experienced this or knows how to get around this?
XPath:
/html/body/div[1]/div/div/form/div[21]/div[3]/button
[HTML]

Could be a synchronization issue. kindly try below solution if your element is not within iframe or else you need to switch control to iframe first before dealing with the web element.
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/div/div/form/div[21]/div[3]/button"))).click();
Or You can also try javascript solution:
WebElement element= driver.findElement(By.xpath("/html/body/div[1]/div/div/form/div[21]/div[3]/button"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);

Try below code -
Actions action = new Actions(driver);
WebElement My_btn = webdriver.findElement(By.xpath("/html/body/div[1]/div/div/form/div[21]/div[3]/button"));
action.moveToElement(My_btn).click(My_btn).build().perform();
Let me know the outcome.

Related

Unable to click or sendKeys in ajax overlay popup coming and its showing Element not Intractable exception using selenium and java

I have been practicing to automate few scenarios for web application way2Automation.com and struggling to enter the text in Registration form coming as the popup.
I have done some research already and tried many ways mentioned below:
a) Using WebDriverWait and explicit wait
b) Using Implicit wait and Thread.sleep
c) Using JavaScriptExecutor
But none of them worked for me and I am still stuck to register the user. Would really appreciate the help. Below are the artificats
URL: http://way2automation.com/way2auto_jquery/index.php
Code trials:
1)
// WebElement ele = driver.findElement(By.xpath("//*[#id='load_form']/div/div[2]/input"));
// JavascriptExecutor executor = (JavascriptExecutor)driver;
// executor.executeScript("arguments[0].click();", ele);
// WebElement button = driver.findElement(By.xpath("//*[#id=\"load_form\"]/div/div[2]/input"));
// new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOf(button));
You need to handle windows
Try the below code and let me know updates
driver.get("http://way2automation.com/way2auto_jquery/index.php");
String parent=driver.getWindowHandle();
String child=driver.getWindowHandle();
driver).switchTo().window(child);
driver.findElement(By.name("name")).sendKeys("Abhishek Saxena");
To click on SUBMIT you can use either of the following Locator Strategies:
cssSelector:
driver.findElement(By.cssSelector("input.button[value='Submit']")).click();
xpath:
driver.findElement(By.xpath("//input[#class='button' and #value='Submit']")).click();
However, as the element is a dynamic element so to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.button[value='Submit']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='button' and #value='Submit']"))).click();
Reference
You can find a detailed discussion on NoSuchElementException in:
NoSuchElementException, Selenium unable to locate element

How to write the selenium code for scroll down the mouse action, I will try with this below code it was

How to write the selenium code for Scroll down the mouse below. I try to use the below code it was not scrolling action.
JavascriptExecutor Scrool = (JavascriptExecutor) driver;
Scrool.executeScript("window.scrollBy(0,250)", "");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
use this method. Call this method with actual webelement where you would like to scroll
public void scrollToElement(WebElement element) {
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
}
#tester
so, first of all, you need to be careful with automation. Before to start the automation check website and be sure that there are no iframes. in your case on your website, you have IFRAME that's why you are getting no such element exception which means your code not reaching this web element.
so to handle this problem you can run it in 2 ways.
the first way to scroll to webelement which you need
driver.get("https://www.abservetechdemo.com/products/rebueats/");
driver.switchTo().frame(0);
WebElement scrollToelement = driver.findElement(By.xpath(" //h2[contains(text(),'Your restaurant, delivered')]"));
((JavascriptExecutor) driver.executeScript("arguments[0].scrollIntoView(true);", scrollToelement);
and the second way just uses your own code but add this driver.switchTo.iframe(0). in your case, you have only 1 iframe so 0 means index of the iframe.
driver.get("https://www.abservetechdemo.com/products/rebueats/");
driver.switchTo().frame(0);
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("scrollBy(0,2500)");
I am sure this will solve your issue I test it couple times

How can I select below element in Selenium Java Web Driver?

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();
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')
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.
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:
The button doesn't belong to and <iframe>, if this is the case - you will have to change the context using switchTo() function
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
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.

click on a button with JQuery

There is somthing wrong with the page I want to test.
My first try:
When I clicked manually on a button, then I will be forwarded normally on the next page.
When I tried to click on the same button with selenium, then I get an error page "Sorry...something gone wrong...blabla". I think this problem can only solve the developer team of the page.
By book = By.cssSelector("#button\\.buchung\\.continue");
//By book = By.cssSelector("button.buchung.continue");
//By book = By.xpath("//*[#id='button.buchung.continue']");
WebElement element= ConfigClass.driver.findElement(book);
element.click();
But I want to try a workaround:
I clicked on the same button with JQuery.
I opened my chrome console and execute the button with:
jQuery('#button\\.buchung\\.continue').click()
How can I execute this JQuery expression in my selenium code?
I tried this, but without success:
JavascriptExecutor je = (JavascriptExecutor) driver;
je.executeScript("jQuery('#button\\.buchung\\.continue').click()");
Use $
je.executeScript("$('#button\\.buchung\\.continue').click()");
jQuery("selector") will return you a list.
I think you have to call click() on the element at index 0 (Assuming exactly one element satisfies the selector)
Code:
je.executeScript("jQuery('#button\\.buchung\\.continue')[0].click()");
You were pretty close. If the cssSelector is uniquely identifying the WebElement you can use the following code block :
By book = By.cssSelector("#button\\.buchung\\.continue");
WebElement element= ConfigClass.driver.findElement(book);
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);

Unable to find draggable web element in selenium webdriver(while other user is able to do so with same code)

I'm trying to test drag and drop functionality using selenium webdriver while writing test scripts using java in eclipse.I'm unable to find the web element that I want to drag while my other friend is able to find the element with same code.
Below is the code we both have used.
WebElement draggable = driver.findElement(By.id("draggable"));
I'm using site www.way2automation.com/demo.html and the page i'm trying to find webelement on, is http://way2automation.com/way2auto_jquery/draggable.php
Please suggest something to find the webelement.
Try this below code, using Action Class.
WebElement from = driver.findElement(By.id("drag"));
WebElement to = driver.findElement(By.id("drop"));
Actions act = new Actions(driver);
act.clickAndHold(from).build().perform();
Thread.sleep(3000);
act.clickAndHold(to).moveToElement(to).release(to).build().perform();
Thread.sleep(2000);
WebElements were not being identified even with right locators because these webElements were under an iFrame so First we need to switch to the iFrame before performing drag and drop. And then we also need to check for the availability of SourceElement and DestinationElements. Below code is working fine for this problem.
//Wait for the frame to be available and switch to it
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector(".demo-frame")));
WebElement Sourcelocator = driver.findElement(By.cssSelector(".ui-draggable"));
WebElement Destinationlocator = driver.findElement(By.cssSelector(".ui-droppable"));
builder.dragAndDrop(Sourcelocator,Destinationlocator).build().perform();
String actualText=driver.findElement(By.cssSelector("#droppable>p")).getText();
Assert.assertEquals(actualText, "Dropped!");

Categories