Selenium driver find element not working - selenium

Selenium web driver finds element unable to find input field on the web page I tried this each and every option XPath, by CSS, by name. the testing site on the local environment.
HTML:
<input class="form-control ng-pristine ng-untouched ng-valid ng-empty" ng-model="email" placeholder="Username" aria-describedby="username" type="text">
xpath:
/html/body/div[2]/div/div/div[1]/div/div/div[2]/div/form/div[2]/div/div/input
css selector:
html.no-js.ng-scope body.pace-done.full-width div#wrapper
div.page-wrapper.white-bg.ng-scope
div.wrapper.wrapper-content.ng-scope div.login-bg.ng-scope
div.container div.row
div.col-sm-6.col-sm-offset-3.col-md-4.col-md-offset-4 div.login-form
form.ng-pristine.ng-valid div.col-sm-12.plr10px div.form-group
div.input-group
input.form-control.ng-pristine.ng-untouched.ng-valid.ng-empty
driver.findElement(By.name("username"))

Here is the Answer to your Question:
As per the HTML you provided, you can use the following xpath to identify the element-
WebElement element = driver.findElement(By.xpath("//input[#ng-model='email' and #placeholder='Username']"));
Incase you are facing an ElementNotVisible exception you can induce ExplicitWait to wait for the element to be clickable as follows:
WebDriverWait wait7 = new WebDriverWait(driver, 10);
WebElement element7 = wait7.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#ng-model='email' and #placeholder='Username']")));
element7.click();
Let me know if this Answers your Question.

Try this, this should work.
WebElement emailInput = new WebDriverWait(driver, 30).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[ng-model='email']")));

JavascriptExecutor je = (JavascriptExecutor) driver;
WebElement Username = driver.findElement(By.xpath("//*[#placeholder='Username' and #type='text'"]));
je.executeScript("arguments[0].scrollIntoView(true);",Username);
Username.sendKeys("Name");

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!

Selenium always gives org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:

The text "Congratulations!" apprears on the site only when the transaction is successful.
I am trying to capture the text of this element using JavascriptExecutor as the type is set to hidden but selenium always displays:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class='form-header']//div[contains(text(),'Congratulations')]"}
(Session info: chrome=85.0.4183.121)
Code 1 (Not Working)
WebElement ele = driver.findElement(By.xpath(//div[#class='form-header']//div[contains(text(),'Congratulations')]));
JavascriptExecutor js = (JavascriptExecutor)driver;
String text = (String)js.executeScript("return arguments[0].value",ele);
System.out.println(text);
Code 2 (Not Working)
WebElement ele = driver.findElement(By.xpath(//div[#class='form-header']//div[contains(text(),'Congratulations')]));
JavascriptExecutor js = (JavascriptExecutor)driver;
String text = (String)js.executeScript("return arguments[0].innerHTML",ele);
System.out.println(text);
The HTML code for this part is as follows:
<input id="hdnWindowLocationHost" name="hdnWindowLocationHost" type="hidden" value="/" data-value="themes/custom">
<div id="sgw" class="sgw SGW-header">
</div>
<div class="page">
<div class="content">
<div class="form">
<div class="form-header">
<div class="headline">Congratulations!</div>
Your password has been reset. Please log in below with your Username and password.
</div>
As the element is a dynamic element so to identify the element you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:
Using cssSelector:
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.form-header > div"))).getText());
Using xpath and text():
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='form-header']/div[text()='Congratulations!']"))).getText());
Using xpath and contains():
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='form-header']/div[contains(., 'Congratulations')]"))).getText());

How to locate the element as per the given HTML

<input value="Order (ESHOP)" class="btn" name="order_eshop" title="Order (ESHOP)" type="button" onclick="Sfdc.logServer('CUSTOM_URL_BUTTON', {id: '00b41000002iuj6', name: 'Order_ESHOP'}, Sfdc.Logging.LogLevel.INFO);
openIntegration('/servlet/servlet.Integration?scontrolCaching=1&lid=00b41000002iuj6&eid=a0863000007SV1q&ic=1&isdtp=vw&linkToken=VmpFPSxNakF4T1MweE1TMHhNRlF5TVRvd09EbzBNaTQyT1RWYSx0eno3X1lSS1lOY1hORmtKb2ZvM3BxLFlXWmtNR0po', 'height=600,location=no,resizable=yes,toolbar=no,status=no,menubar=no,scrollbars=1', 1)">
I' am not able to find the xpath for this. I have same element on a single path Order (ESHOP). When I'am trying to use indexing it is not finding the correct xpath.
The desired element is a JavaScript enabled element so to locate/interact with the element you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following solutions:
Using CSS_SELECTOR:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.btn[name='order_eshop'][value='Order (ESHOP)'][title='Order (ESHOP)']")));
Using XPATH:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='btn' and #name='order_eshop'][#value='Order (ESHOP)' and #title='Order (ESHOP)']")));

Selenium html button not clicking

This is my HTML code
<button type="button" class="button primary" id="submitID" aria-label="Verify and proceed to next step.">
Verify
</button>
This is my selenium code:
WebElement verifyButton = driver.findElement(By.id("submitID"));
verifyButton.click();
The button comes to focus but not clicking.
Try this in case there are multiple elements with the same id:
WebElement verifyButton = driver.findElement(By.xpath(".//button[#id='submitID']"));
You can use javascript executor . Please ref below code
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementById('submitID').click();");
You can use any of this thing here
Using ID
WebElement verifyBtn = driver.findElement(By.id("submitID"));
verifyBtn.click();
Using class name
WebElement verifyBtn = driver.findElement(By.className("button primary"));
verifyBtn.click()
Using xpath
WebElement verifyBtn = driver.findElement(By.xpath("//button[#aria-label='Verify and proceed to next step.' and #id='submitID']"));
verifyBtn.click()
Using javascriptExecutor
WebElement verifyBtn = driver.findElement(By.id("submitID"));
JavascriptExecutor js=(JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", verifyBtn );
If ID is unique, you might wanna wait for some time and then click on it.
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("submitID"))).click();
OR cssSelector :
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[type='button'][id='submitID']"))).click();
OR XPATH :
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Verify']"))).click();
OR XPATH :
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(#aria-label,'Verify and proceed to next step.')]"))).click();
what is the output of this? note - findElementS
driver.findElements(By.id("submitID")).size()
for sure there are many buttons with same id, which must not happen in valid html

Selenium radiobutton selection

I have an HTML code as follows:
<label class="top" for="id_gender2">
<div id="uniform-id_gender2" class="radio">
<span>
<input id="id_gender2" type="radio" value="2" name="id_gender">
</span>
</div>
Mrs.
</label>
The radio button is getting selected after mouse hover.
I have tried with all possible attributes for selection but I am getting element not found exception.Please let me know how to write java script in webdriver.
Try following code and let me know the result:
Actions action = new Actions(driver);
WebElement hover = driver.findElement(By.xpath("//*[#class='radio hover']"));
action.moveToElement(hover).moveToElement(driver.findElement(By.xpath("//input[#id='id_gender2']"))).click().build().perform();
UPDATE
Actually there is no need in mouse hover. Target radio seem to be initially non-clickable, so you just need extra time to wait for page complete rendering:
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#id_gender2")));
driver.findElement(By.cssSelector("input#id_gender2")).click();
Try javascript executor as follow:
WebElement element = driver.findElement(By.id("id_gende‌​r2"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
Your element is probably not loaded properly while getting error. You'd better use explicit wait in your script. Wait until your specific element loaded to be visible/clickable. I think, this may help you.
Code snippet:
By yourElementToSelect = By.id("id_gender2");
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(yourElementToSelect));
driver.findElement(yourElementToSelect).click();