How to locate elements on a javascript form using Selenium WebDriver - selenium

I have a page that renders a login form. Using Firefox's firebug I have identified the username id = txtUsername and the password text box is txtPassword. Everything is fine in Firefox. BUT moving over to IE Explorer browser, am getting error and cant locate the elements.
On inspecting the page, I found the form is being rendered by a JavaScript function. Below is the html:
form name="frmLogin" id="frmLogin" onsubmit="return $('#frmLogon').valid();" action="default.aspx" method="post" jQuery172043="1" novalidate="novalidate"
How can I locate the page elements using selenium webdriver.
This is the code I have written.
/* setup for IE. Remove if not needed */
File file = new File("C:/SeleniumJavaLibrary/selenium-2.28.0/IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability("ignoreZoomSetting", true);
WebDriver driver = new InternetExplorerDriver(caps);
driver.manage().timeouts().implicitlyWait(28,TimeUnit.SECONDS );
/*setup for IE*/
driver.get("http://website.com");
WebElement element = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('#frmLogin').valid();");
element.findElement(By.name("txtUsername")).sendKeys("user");
element.findElement(By.name("txtPassword")).sendKeys("password");

I would do the following:
WebDriverWait waiting = new WebDriverWait(driver, 15, 100);
WebElement element = waiting.until(ExpectedConditions.visibilityOfElementLocated(By.id("frmLogin")));
This will create a WebDriverWait object that will wait up to 15 seconds, checking to see if the expected condition is valid every 100 milliseconds. This object is then used to wait until the frmLogin element is visible on the page, and then returns you a WebElement that can be used later on in your test (if required).

Related

Caused by: org.openqa.selenium.ElementClickInterceptedException: element click intercepted: [duplicate]

I have a project that I am working on with java and selenium.
the test work OK in UI mode.
However in headless mode I get this error
org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <label _ngcontent-yrc-c26="" formcontrolname="reportingDealPermission" nz-checkbox="" class="ant-checkbox-wrapper ng-untouched ng-pristine ng-valid" ng-reflect-name="reportingDealPermission">...</label> is not clickable at point (161, 562). Other element would receive the click: <div _ngcontent-yrc-c26="" class="footer">...</div>
how can I resolve this issue (working in UI mode). this is my code
WebDriver driver = getWebDriver();
WebElement element;
Thread.sleep(60000);
element = driver.findElement(By.xpath("//label[#formcontrolname='reportingDealPermission']"));
element.click();
why in selenium there is no operation to move to the element and break all layers.
this is the UI.
this is working in UI mode not working in headless mode, made sleep for 6 minutes and not resolved so this is not time issue
This error message...
org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <label _ngcontent-yrc-c26="" formcontrolname="reportingDealPermission" nz-checkbox="" class="ant-checkbox-wrapper ng-untouched ng-pristine ng-valid" ng-reflect-name="reportingDealPermission">...</label> is not clickable at point (161, 562). Other element would receive the click: <div _ngcontent-yrc-c26="" class="footer">...</div>
...implies that the click on the desired element was intercepted by some other element.
Clicking an element
Ideally, while invoking click() on any element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("label[formcontrolname=reportingDealPermission][ng-reflect-name=reportingDealPermission]"))).click();
xpath:
new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#formcontrolname='reportingDealPermission' and #ng-reflect-name='reportingDealPermission']"))).click();
Update
After changing to headless if it still doesn't works and still get exception there still a couple of other measures to consider as follows:
Chrome browser in Headless mode doesn't opens in maximized mode. So you have to use either of the following commands/arguments to maximize the headless browser Viewport:
Adding the argument start-maximized
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("start-maximized");
WebDriver driver = new ChromeDriver(options);
Adding the argument --window-size
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--window-size=1400,600");
WebDriver driver = new ChromeDriver(options);
Using setSize()
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
WebDriver driver = new ChromeDriver(options);
driver.manage().window().setSize(new Dimension(1440, 900));
You can find a detailed discussion in Not able to maximize Chrome Window in headless mode
Additionally, you can also wait for the intercept element to be invisible using the ExpectedConditions invisibilityOfElementLocated before attempting the click() as follows:
cssSelector:
new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("div.footer")));
new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("label[formcontrolname=reportingDealPermission][ng-reflect-name=reportingDealPermission]"))).click();
xpath:
new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[#class='footer']")));
new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#formcontrolname='reportingDealPermission' and #ng-reflect-name='reportingDealPermission']"))).click();
References
You can find a couple of related relevant discussions in:
Selenium Web Driver & Java. Element is not clickable at point (x, y). Other element would receive the click
Element MyElement is not clickable at point (x, y)… Other element would receive the click
Try adding an explicit wait
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#formcontrolname='reportingDealPermission']"))).click();
and if this doesn't work then try using the JS Executor
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#formcontrolname='reportingDealPermission']")));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);
None of the above answers worked for me. Try using action class as follows:
WebElement element = driver.findElement(By.xpath("//div[#class='footer']"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();
For this issue:
org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <label _ngcontent-yrc-c26="" formcontrolname="reportingDealPermission" nz-checkbox="" class="ant-checkbox-wrapper ng-untouched ng-pristine ng-valid" ng-reflect-name="reportingDealPermission">...</label> is not clickable at point (161, 562).
Another element receives the click:
Answer is to explicitly wait with javascript executor. This combination is working for me:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#formcontrolname='reportingDealPermission']")));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);
WebDriverWait wait = new WebDriverWait(driver, 10); ---> has deprecated and it gives an error. Please use below instead:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#formcontrolname='reportingDealPermission']"))).click();
Ensure to import relevant items for the WebDriverWait and ExpectedConditions.
In my case "JavaScript" works:
WebElement ele = driver.findElement(By.xpath("(//input[#name='btnK'])[2]"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click()", ele);
It was taken from:
http://makeseleniumeasy.com/2020/05/25/elementclickinterceptedexception-element-click-intercepted-not-clickable-at-point-other-element-would-receive-the-click/
Answer is worked for me.
Please check the below sreenshot for the my problem reference.
Below alert is comes in between the the my button and frame.
enter image description here
In my case, nothing worked. Only the THREAD.SLEEP() method worked!

How to click on save button in chrome print privew page using selenium Java

I am currently looking for a solution to click on Sava button in chrome print preview window with selenium Java.
Is there any way we can handel chrome print preview page?
I have tried with the Robot class, but it seems not reliable/stable for my application.
Could you please someone help me to achieve this with selenium Java.
I fail to see any "Save" button on print preview page for Chrome browser
Here is how you can click "Cancel" button, I believe you will be able to amend the code to match your requirements:
First of all make sure to wait for the preview page to be available using Explicit Wait
Change the context to the print preview window via WebDriver.switchTo() function
All the elements at the print preview page are hidden in ShadowDom therefore you will need to:
Locate the element which is the first parent of the element you're looking for
Get its ShadowRoot property and cast the result to a WebElement
Use the WebElement.findElement() function to locate the next parent
repeat above steps until you reach the desired button
Example code just in case:
new WebDriverWait(driver, 10).until(ExpectedConditions.numberOfWindowsToBe(2));
driver.switchTo().window(driver.getWindowHandles().stream().skip(1).findFirst().get());
WebElement printPreviewApp = driver.findElement(By.tagName("print-preview-app"));
WebElement printPreviewAppConten = expandShadowRoot(printPreviewApp, driver);
WebElement printPreviewSidebar = printPreviewAppConten.findElement(By.tagName("print-preview-sidebar"));
WebElement printPreviewSidebarContent = expandShadowRoot(printPreviewSidebar, driver);
WebElement printPreviewHeader = printPreviewSidebarContent.findElement(By.tagName("print-preview-header"));
WebElement printPreviewHeaderContent = expandShadowRoot(printPreviewHeader, driver);
printPreviewHeaderContent.findElements(By.tagName("paper-button")).get(1).click();
where expandShadowRoot function looks like:
private WebElement expandShadowRoot(WebElement parent, WebDriver driver) {
return (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].shadowRoot", parent);
}
Save button will not work because page is not part of webpage. selenium only support web based application.
But you can use SIKULI to handle above scenario.

PartialLink text for selecting value in selenium

Scenario: Search google with word 'Selenium' and click on the first link.
I have written this below code:
`WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.findElement(By.id("lst-ib")).sendKeys("Selenium");
List<WebElement> alist = driver.findElements(By.partialLinkText("Selenium"));
System.out.println(alist.size());`
but it is giving me size as zero. Why?
Actually when you are going to find list of all links with partial text Selenium, all links are not present in the DOM due to fast execution. You should try using WebDriver to wait until all elements visible in the DOM as below :-
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.findElement(By.id("lst-ib")).sendKeys("Selenium");
WebDriverWait wait = new WebDriverWait(driver, 10);
List<WebElement> alist = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.partialLinkText("Selenium")))
System.out.println(alist.size());
Output:-13
Hope it helps...:)
ur problem occurs because u don't wait after searching. just wait a little moment so that browser can search and show the result. use the below code :
driver.findElement(By.id("lst-ib")).sendKeys("Selenium");
Thread.sleep(5000);
though this kind of wait is not recommended to use. Try to use wait by using until like:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("element css path")));
u can use by xpath or id or class here.

Popup scroll not working in selenium webdriver

I am running my script in mozilla firefox I want to scroll popup I
applied so many methods but doesn't work for me
I used keys.tab to reach that element but it was unable to enter text in that textfield using senkeys("xyz#gmail.com)
I used scroll method
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('youama-email').scrollIntoView(true);");
some exception occur
3.I used Moveto element method but got exception
WebElement element = driver.findElement(By.id("youama-email"));
Actions
actions = new Actions(driver);
actions.moveToElement(element);
actions.click();
actions.perform();
// Initialize Javascript executor
JavascriptExecutor js = (JavascriptExecutor) driver;
// Scroll inside web element vertically (e.g. 100 pixel)
js.executeScript("arguments[0].scrollTop =
arguments[1];",driver.findElement(By.id("<div-id>")), 100);
Please help me to scroll and enter into the email as well as other
fields that will appear after scroll [![enter image description
here][1]][1]
[1]: http://i.stack.imgur.com/D0hqI.png
Try this code. I think what you didn't do was to wait for the element to be visible which i did. See the below code. It is running correctly.
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://wyomingvapor.com/");
driver.findElement(By.xpath(".//*[#id='header']/div/div[2]/div/a[1]")).click();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='y-to-register']/input")));
driver.findElement(By.xpath(".//*[#id='y-to-register']/input")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='youama-firstname']")));
driver.findElement(By.xpath(".//*[#id='youama-firstname']")).sendKeys("xyz#gmail.com");
Thread.sleep(2000L);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyPress(KeyEvent.VK_X);
robot.keyRelease(KeyEvent.VK_X);
robot.keyPress(KeyEvent.VK_Y);
robot.keyRelease(KeyEvent.VK_Y);
robot.keyPress(KeyEvent.VK_Z);
robot.keyRelease(KeyEvent.VK_Z);
If you still stuck then do reply to me, Jyotsana.
Happy Learning :-)

Selenium is showing element not found exception

I am able to do validation on first page using selenium. But when i am transferring to next page than it is showing above error.
Code is:-
WebDriver driver = new FirefoxDriver();
String baseUrl = "http://192.168.1.47";
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
driver.get(baseUrl);
selenium.open("http://192.168.1.47");
selenium.type("username","clnt123822");
selenium.type("birthday", "150347");
String name = driver.findElement(By.name("username")).getAttribute("value").toString();
String dob = driver.findElement(By.name("birthday")).getAttribute("value").toString();
System.out.println(name);
System.out.println(dob);
selenium.click("btnsubmit");
// After pressing this button it is going to next page where i am having select
html tag.At this point error is coming
Exception in thread "main" com.thoughtworks.selenium.SeleniumException: Element select1 not found
This is second page checking which is throwing error
selenium.type("select1","r");
String select1 = driver.findElement(By.id("sel1")).getAttribute("value").toString();
System.out.println(select1);
I guess you need to wait until the element in the second page to be visible/clickable. Try this code just after hitting the submit button in page one.
WebElement element = new WebDriverWait(Driver,30).until(ExpectedConditions.elementToBeClickable(By.id("sel1")));