Selenium moveToElement() is not working - java

After testing logging in https://www.pcbway.com/ I want to test logout.
In order to do this I need to hover on a div so that the sign out button appears. I tried using
Actions actions = new Actions(driver);
actions.moveToElement(element).build().perform();
but it did not work. I tried using the javascript exector
JavascriptExecutor executor = (JavascriptExecutor)driver; executor.executeScript("arguments[0].click();", element);
but it also did not work. I can see that the hover works by using mouseout jquery (screenshot below)
I am using chrome 58 with selenium 3.4.0.

You can try to force it open by adding the class the element receives when the menu is open.
executor.executeScript("$('.nav-user-account').addClass('user-account-unfold')")
Then you can click the element in the menu.

Related

Can't click on Submit button, not working in automation testing, works manually

I have a standard form with 2 text boxes to fill and a submit button. The Submit button suppose to work after filling in the first mandatory text box.
It works manually, but when running on the automation infrastructure, the element doesn't get clicked.
The odd thing is that when debugging, the submit button is not clickable too, although it's not greyed out.
I tried the 3 classic methods:
Javascript:
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
Actions:
Actions actions = new Actions(driver);
actions.moveToElement(element).click().perform();
click:
element.click()
**Not working manually while in automation **
only when closing the form and creating a new one it works.
To click() on any clickable element ideally you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following solutions:
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//elementXpath"))).click();
As an alternative, using Actions class:
new Actions(driver).moveToElement(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//elementXpath")))).click().build().perform();
Another alternative, using JavascriptExecutor:
((JavascriptExecutor)driver).executeScript("arguments[0].click();", new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//elementXpath"))));
According to your steps an replays , may be there is a hidden popup or something block your elements ,so use WebDriverWait not only clickable but try to elementToBeSelected() , invisibilityOfTheElementLocated() or
presenceOfAllElementsLocatedBy()
increase the waiting time to be able to detect the issues

Xpath selector not working in IE but working fine in Chrome and Firefox

I am using following xpath to click on element using JSExecutor in Selenium webdriver. This works fine in Firefox and chrome but does not work in IE.
Any idea to make this work? After lot of trial and error I have made this work in FF and chrome and have come up with the following XPath.
//*[contains(#class,'ui-select-choices-row') or contains(#id,'ui-select-choices-row')]//*[text()='TextofMyElementToBeclicked'
Additional info: This is a Jquery drop down on an angularJS application. When the user clicks on the drop down //ul is loaded and i am using the above xpath (which is part of //ul) to select the element based on text (using Javascript executor click). I used JS executor because, click() function in selenium simply could not click on the drop down element.
I am clicking element using below.
WebElement element = driver.findElement(By.xpath("YourNumbersXpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
enter code here
I successfully tested your XPath with IE11, so it's not an issue related to IE. It's most likely a timing issue. First click on the drop button, then wait for the targeted element to appear and finally click on it:
WebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.get("...");
// move the cursor to the menu Product
WebElement element = driver.findElement(By.xpath("drop down button")).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("drop down item"))).click();
IE11 seems to struggle with contains(#class and possibly also the contains(#id. Try using alternative solutions like starts-with.

Issue with focus in ie browser using selenium webdriver code

Firstly i need to mouse over and then it opens a drop down then i will click on that link.Its working fine in firefox,chrome and issue is in ie.
Here is the code
WebElement element=driver.findElement(By.xpath("/html/body/div/span/form[2]/div[1]/div[1]/div[3]/div[2]/ul/span[3]/li/a"));
Actions act=new Actions(driver);
act.moveToElement(element).build().perform();
WebElement element2=driver.findElement(By.xpath("/html/body/div/span/form[2]/div[1]/div[1]/div[3]/div[2]/ul/span[3]/li/ul/span[1]/li/a"));
Actions act1=new Actions(driver);
//act1.click(element2);
act1.moveToElement(element2).click(element2).build().perform();
It works even in IE browser, when we use this piece of code
caps.setCapability("requireWindowFocus", true);
If we use requiredwindow focus its working fine even in IE browser so no issues with locators
But am not encouraged to use above requiredwindowfoucs code in my project.
Is there any other way to do it.
The issue in Ie browser when we are not using requiredwindowfocus, it is clicking on some other link, so am assuming the problem is with focus.
so kindly help me with this issue without using requiredfoucswindow
I have has similar issues in my project. In IE, clicking drop down values does not work. I have a pretty weird and unexpected solution for this. Find the element before performing the operations. I am assuming that this is required because when you move to element1 and when trying to find element the IE Xpath processor messes up the focus.
WebElement element=driver.findElement(By.xpath("/html/body/div/span/form[2]/div[1]/div[1]/div[3]/div[2]/ul/span[3]/li/a"));
WebElement element2=driver.findElement(By.xpath("/html/body/div/span/form[2]/div[1]/div[1]/div[3]/div[2]/ul/span[3]/li/ul/span[1]/li/a"));
Actions act=new Actions(driver);
act.moveToElement(element).build().perform();
act.moveToElement(element2).click(element2).build().perform();
Please try this and let me know if this works for you too!!
Also, consider using relative XPaths instead of the absolute XPaths you have used. The second Action is not required. One action should do the trick.
EDIT Based on user comment
Move to element1 is working and the list is getting displayed. So I am assuming that element2 is visible and so we can directly click on it without using the Actions class
Actions act=new Actions(driver);
act.moveToElement(element).build().perform();
element2.click();

How can i create mouse event for clicking a tab- Selenium WebDriver?

Working with Selenium WebDriver and Java. I have tab name called PR Per Product.
I have many tabs under PR Reports tab.. In that PR tab i used
WebElement menuHoverLink = driver.findElement(By.id("ext-pr"));
actions.moveToElement(menuHoverLink).perform();
But the same format i tried to click PR Per Product tab it is clicking some other tab. The element is targeting through the ID.
<a id="ext-pr-domain" class=" ext-pr-domain" name="ext-pr-domain" href="https://10.4.16.159/extranet_prperdomain/reports/">Per Prod/SW Pack/RDT Group</a>
If i use this method it is exactly clicking the tab. But the problem it is not hovering and clicking.
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", driver.findElement(By.id("Per Prod/SW Pack/RDT Group") ));
Please help me for the issue..
As far as i know, you have to build the action also. Try
actions.moveToElement(menuHoverLink).build().perform();
or if you need to click in order to make it visible:
actions.moveToElement(menuHoverLink).click().build().perform();

Workarounds for Selenium not clicking button with InternetExplorerDriver

I've got a button on a webpage that Webdriver will not click when I'm running via IE - I've tried the below workarounds but no luck -
Clicking via Javascript:
((JavascriptExecutor) driver).executeScript("$(arguments[0]).click()", webElement)
Using SendKeys:
webElement.SendKeys(keys.Enter)
Using the Action Builder
Actions test = new Actions(driver);
test.moveToElement(webElement);
test.clickAndHold();
test.release();
test.build();
test.perform();
Making sure the window is the active one, then clicking on the parent object, then the object itself
Problem is, none of them work. I've checked in Firefox and Chrome and the script runs fine. I've confirmed that the element is being found when using IE. Are there any other workarounds I can try?
Seems you are tyring to use JQuery style click... normal javascript style click should work.
Try this:
((JavascriptExecutor) driver).executeScript("arguments[0].click();", webElement)
I always found successful with the following for clicking an element in IE.
If is a checkbox/radio: webElement.click();
Clickable input element: webElement.sendKeys("\n");
For other elements, use the above said JS style click.
If the button is a form submit button, you can use : webElement.submit() on another field of the form.

Categories

Resources