Error in finding xpath for a sub-element in HTML - java

I am using below code
Actions action=new Actions(driver);
action.moveToElement(facultyOfCivil).build().perform();
WebElement oceanManagement=driver.findElement(By.xpath("//a[#href='https://www.annauniv.edu/iom/home.php']"));
action.moveToElement(oceanManagement).build().perform();
oceanManagement.click();
After hovering the mouse on an element, i am not able to find xpath for sub-element as the sub-element is not being displayed in HTML

Use JavaScriptExecutor to click the element. Try this one,
WebDriverWait webDriverWait = new WebDriverWait(driver, 10);
Actions actions = new Actions(webDriver);
actions.moveToElement(facultyOfCivil).build().perform();
WebElement oceanManagement = webDriverWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id="menuItemHilite32"]")));
((JavascriptExecutor) webDriver).executeScript("arguments[0].scrollIntoView();", oceanManagement);
((JavascriptExecutor) webDriver).executeScript("arguments[0].click();", oceanManagement);

Related

Button not clickable consistently in Selenium Java

I am trying to click a button in selenium java code and it is not clicking all the time. Apparently this is pretty common issue.
I tried below few solutions:
HTML Code :
<button class="btn btn--action btn--border-white btn--my__calculate" style="display: inline-block;">Final Figure</button>
Solution 1:
WebElement btnWorkout = webDriver.findElement(By.cssSelector(".btn--my__calculate"));
if (btnWorkout.isDisplayed() && btnWorkout.isEnabled()) {
btnWorkout.click();
}
Solution 2 :
WebDriverWait wait = new WebDriverWait(webDriver, 10);
WebElement btnWorkout = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(".btn--my__calculate")));
btnWorkout.click();
Solution 3:
WebElement btnWorkout = webDriver.findElement(By.cssSelector(".btn--my__calculate"));
JavascriptExecutor executor = (JavascriptExecutor) webDriver;
executor.executeScript("arguments[0].click();", btnWorkout);
None of them worked for me.
Other strange thing is above step passes without an error and button doesn't click as expected
Induce WebDriverWait And Following Xapth.
Try following options.
Option1:
WebDriverWait wait = new WebDriverWait(webDriver, 20);
WebElement btnWorkout=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='btn btn--action btn--border-white btn--my__calculate'][text()='Final Figure']")));
btnWorkout.click();
Option2: Use Action class
WebDriverWait wait = new WebDriverWait(webDriver, 20);
WebElement btnWorkout=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='btn btn--action btn--border-white btn--my__calculate'][text()='Final Figure']")));
Actions action=new Actions(webDriver);
action.moveToElement(btnWorkout).click().build().perform();
Option3: Use JavaScript Executor
WebDriverWait wait = new WebDriverWait(webDriver, 20);
WebElement btnWorkout=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='btn btn--action btn--border-white btn--my__calculate'][text()='Final Figure']")));
JavascriptExecutor executor = (JavascriptExecutor) webDriver;
executor.executeScript("arguments[0].click();", btnWorkout);
Curious that the executeScript() option didn't work for you. Keep using your WebDriverWait from option 2, but instead of the click() method, try using Action Chains. I'm not sure how to write this in Java, but it'll be something like this:
Actions action = new Actions(driver);
action.move_to_element(btnWorkout).click(btnWorkout).perform();
Maybe you have another hidden element with same CSS (class). You can try capture the element by linkText or partialLinkText like this:
WebElement btnWorkout = webDriver.findElement(By.linkText("Final Figure"));

Problems to click in a JQUERY element using Selenium Webdriver

I'm trying to click in some JQUERY elements from a very known website to practice Selenium (http://the-internet.herokuapp.com/jqueryui/menu).
I figured out how to navigate into the menu (not sure if my code is a good solution), however am not being able to click in each last submenu option (PDF, CSV, Excel)
I'm trying something like below:
Actions builder = new Actions(driver);
Action mouseOverMenu;
mouseOverMenu = builder.moveToElement(driver.findElement(By.id("ui-id-2"))).build();
mouseOverMenu.perform(); //accessing Enabled menu option
mouseOverMenu = builder.moveToElement(driver.findElement(By.id("ui-id-4"))).build();
mouseOverMenu.perform(); //accessing Downloads submenu option
String jQuerySelector = "$('a#ui-id-6.ui-corner-all')";
WebElement webElement = (WebElement) ((JavascriptExecutor) driver).executeScript("return $(" + jQuerySelector+ ").get(0);");
//click() also did not work
WebElement webElement = (WebElement) ((JavascriptExecutor) driver).executeScript("return $(" + jQuerySelector+ ").click();");
Your JavaScript function of click is wrong.
Use below javascript syntax
executor.executeScript("arguments[0].click();", WebElement);
Below code works for me:
Actions builder = new Actions(driver);
Action mouseOverMenu;
mouseOverMenu = builder.moveToElement(driver.findElement(By.id("ui-id-2"))).build();
mouseOverMenu.perform(); //accessing Enabled menu option
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ui-id-4")));
wait.until(ExpectedConditions.elementToBeClickable(By.id("ui-id-4")));
mouseOverMenu = builder.moveToElement(driver.findElement(By.id("ui-id-4"))).build();
mouseOverMenu.perform(); //accessing Downloads submenu option
WebElement webElement2= driver.findElement(By.cssSelector("a#ui-id-6.ui-corner-all")); // #ui-id-6 is for pdf, #ui-id-7 csv so on
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", webElement2);

Canceled page load listener because no navigation has been detected

Unable to click a button . Firefox version 56.0
WebDriver wd = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor) wd;
wd.get("https://www.seleniumeasy.com/test/basic-first-form-demo.html");
wd.findElement(By.xpath("//input[#id='sum1']")).sendKeys("5");
wd.findElement(By.xpath("//input[#id='sum2']")).sendKeys("10");
WebElement e1 = wd.findElement(By.xpath("//button[#class='btn btn-
default']"));
js.executeScript("arguments[0].scrollIntoView();",e1 );
Thread.sleep(10000);
e1.click();
I want to click 'Get Total' button but "Canceled page load listener because no navigation has been detected" message is showing.
Induce WebDriverWait and elementToBeClickable() and following xpath
wd.get("https://www.seleniumeasy.com/test/basic-first-form-demo.html");
WebDriverWait wait = new WebDriverWait(wd, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='sum1']"))).sendKeys("5");
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='sum2']"))).sendKeys("10");
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='btn btn-default'][text()='Get Total']"))).click();
To click() on the element with text as Get Total you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
WebDriver wd = new FirefoxDriver();
wd.get("https://www.seleniumeasy.com/test/basic-first-form-demo.html");
new WebDriverWait(wd, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.form-control#sum1"))).sendKeys("5");
wd.findElement(By.cssSelector("input.form-control#sum2")).sendKeys("5");
wd.findElement(By.cssSelector("button.btn.btn-default[onclick^='return total']")).click();
xpath:
WebDriver wd = new FirefoxDriver();
wd.get("https://www.seleniumeasy.com/test/basic-first-form-demo.html");
new WebDriverWait(wd, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='form-control' and #id='sum1']"))).sendKeys("5");
wd.findElement(By.xpath("//input[#class='form-control' and #id='sum2']")).sendKeys("5");
wd.findElement(By.xpath("//button[#class='btn btn-default' and text()='Get Total']")).click();
Browser Snapshot:

How can i select text from one document using selenium webdriver

Suppose i have one document then how can i select text from that document using Selenium WebDriver.
I have entered below code:
d1.findElementByClassName("odd").click();
Thread.sleep(5000);
d1.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
WebElement el = d1.findElement(By.id("doc-content"));
Actions act = new Actions(d1);
act.clickAndHold(el).build().perform();
act.release().perform();
act.doubleClick(el).build().perform();
act.clickAndHold(el).build().perform();
WebElement el1 = d1.findElement(By.id("doc-data"));
act.moveToElement(el1, 50, 50).build().perform();
act.dragAndDropBy(el, 100, 150).build().perform();
act.release().build().perform();
If I understand correctly you want to Highlight found element, you can try to run js to change style for that element:
WebElement element = driver.FindElement(By.Id('someId'));
((JavascriptExecutor) driver).executeScript("arguments[0].style.backgroundColor='yellow';", element);
Or change font color:
((JavascriptExecutor) driver).executeScript("arguments[0].style.color='yellow';", element);

clicking next button on reddit with selenium not doing anything

private void next() {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.reddit.com/r/pics/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement element = driver.findElement(By
.xpath("//span[contains(.,'next')]"));
element.click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println(driver.getCurrentUrl());
is this code correct, all that happens when next is clicked is the focus scrolls down the page to the button
Your xpath selector is wrong.
Change it to:
WebElement element = driver.findElement(By
.xpath("//a[contains(text(),'next')]"));
or even better (in case one of the topic links contains the text "next") use:
WebElement element = driver.findElement(By
.xpath("//span[#class='nextprev']/a[contains(text(), 'next')]"));
This will ensure that the a element that is picked up is within the correct span at the bottom of the page and make your test less brittle.
The xpath = //span[contains(.,'next')] used in your code, locates span with contents view more : next › ,but you need to click only on next ›.i.e., you need to click on the anchor tag which contains next ›.
The below code will solve the issue.
WebDriver driver = new FirefoxDriver();
driver.get("http://www.reddit.com/r/pics/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement element = driver.findElement(By
.linkText("next ›"));
element.click();
System.out.println(driver.getCurrentUrl());
It is always better to avoid xpath and use other locators like linkText or partialLinkText in this case.

Categories

Resources