Unable to locate element - Java/Selenium - java

I'm trying to select the highlighted element below (which is a "window close" button):
There is one other element with class='icon-Dismiss' on the page, but none with class='dialog-close'.
What I have tried so far:
driver.findElement(By.xpath("//*[#class='icon-Dismiss' and #class='dialog-close']"))
driver.findElement(By.className("dialog-close"))
driver.findElement(By.xpath("//*[#id='contentBox']"))
In all cases however, I receive the following error:
no such element: Unable to locate element
Does anyone have an idea on how I can select this element?

You need to induce WebDriverWait for the desired elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.contentBox div.icon-Dismiss.dialog-close"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#id='contentBox']//div[#class='icon-Dismiss dialog-close']"))).click();

Try using
driver.findElement(By.cssSelector(".icon-Dismiss.dialog-close"))
Also, make sure this element is not in an iFrame. If it is then you will need to switch to the iFrame first before you can find the element.

try this driver.switchTo().alert().dismiss(); if that dialog box is an alert.

If you just want to locate then
driver.findElement(By.cssSelector("div.icon-Dismiss.dialog-close"));
but if you want to just close the alert then alert().dismiss(); will be the best option.

If this is the window pop up
try : driver.switchTo().alert().dismiss();
if this is application these locators should work
Note : make sure you are using waiting till popup appearing on page

Related

Selenium not able to input element in the form inside fieldset tag

I have tried a different method to input elements like
Here is xpath I used
By NameOfTheProperty=By.xpath("//fieldset/div/input[#name='name']");
By NameOfTheProperty=By.xpath("//div/input[#name='name']");
By NameOfTheProperty=By.xpath("//input[#name='name']");
Tried with Selenium Builder
WebElement element=driver.findElement(by);
Actions builder = new Actions(driver);
Action mouseOverHome = builder
.moveToElement(element)
.click().sendKeys(text).build();
mouseOverHome.perform();
Tried with
WebElement element=driver.findElement(by);
element.sendKeys(text);
None of the methods is working..I can not able to input text inside the field and It shows
Element not interactable
Here is the site
I hope someone help me to find out the solution..
Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.
xpath you should checks is :
//input[#name='name']
if it is unique, then you can use Javascript executor :
WebElement password_input = driver.findElemenet(By.xpath("//input[#name='name']"));
((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute('value', 'password_should_be_written_here')", password_input)
personally I would use id's if the element has one.
"//*[#id='__BVID__104']"
Make sure you are waiting for the element to be ready.
WebDriverWait wait = new WebDriverWait();
wait.until(ExpectedConditions.elementToBeClickable(element));
If it then times out on wait.until(... then I've found that sometimes an element must first be clicked for it to be exposed.
If this is the case. You will have to inspect the element before it is clicked. Find it's xpath, and see if it changes when it is clicked. If so then create a webElement for that element and have Selenium first click it, before clicking the actual field/element.

How to click on the reCaptcha checkbox

I have an issue with located correct Xpath/ID input field, I am taking the following website as an example:
https://garden.lovetoknow.com/vegetable-garden/how-ripen-green-tomatoes-off-vine
After you open the link then scroll down a bit, you will see "write a comment" blue button, click it then fill the text and name, it will appear a reCaptcha example, I tried the following way to click the checkbox, but without success. I would really appreciate if someone can let me a hand on it
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath(
"//iframe[starts-with(#name, 'a-') and starts-with(#src, 'https://www.google.com/recaptcha')]")));
new WebDriverWait(driver, 10)
.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.recaptcha-checkbox-checkmark"))).click();
Seems you were close enough. The name of the <iframe> i.e. a-x2cp4vfbaqu8 looks dynamic, so it would be better to avoid the name attribute and you can use either of the following Locator Strategies:
Using css_selector:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("iframe[src^='https://www.google.com/recaptcha/api2/anchor']")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("span#recaptcha-anchor"))).click();
Using xpath:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(#src, 'https://www.google.com/recaptcha/api2/anchor')]")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("//span[#id='recaptcha-anchor']"))).click();
References
You can find a couple of relevant discussions in:
How to click on elements within an iframe to enable the captcha through the audio using Selenium and Python
Find the reCAPTCHA element and click on it — Python + Selenium
Dealing with reCAPTCHA in Python Selenium
I struggled with this for several days, it's actually very simple. The captcha is located in a frame, and in order to click on it, you need to switch to this frame.
I did it like this with Selenium:
driver.switchTo().frame(driver.findElement(By.xpath("//*[#title='reCAPTCHA']")));
driver.findElement(By.xpath("//span[#id='recaptcha-anchor']")).click();

How to identify the element with text as Sign in with Google through xpath in java selenium

I am performing Automation for a redbus application but I am finding the web elements using firebug and it will highlight the firefox browser but it doesn't work in a chrome browser.
Please review the following screenshots:
Redbus website link
step 1 Open redbus application URL =https://www.redbus.in/
step 2 Click Accounts module
step 3 Click sign or signup link
My XPath is :
.//*[#id='g-signin2']//span[text()='Sign in with Google']
Chrome browser screenshot: Can not identify the webElement
Google sign up link is in Iframe , in order to interact with elements which are inside the frame/iframe , you need to change the focus of your web driver to that particular frame.
How you can do that in your case :
driver.switchTo.frame("//iframe[#class='modalIframe']")
Then you can interact with Sign in with Google.
WebElement signupButton = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[contains(#id,'signed') and text()='Sign in with Google']")))
signupButton.click();
HTH !
To identify and invoke click() on the element with text as Sign in with Google, as the element is within an . So you have to induce WebDriverWait for both the cases, once for the frame to be available and again for the desired element to be clickable and you can use the following solution :
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[#class='modalIframe' and #src='/account?pageName=Home&noReload=noReload']")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[#class='abcRioButtonContents']//span[normalize-space()='Sign in with Google']"))).click();
What You have here is modal frame, You should first switch on it, and the do actions on elements:
driver.switchTo().frame("modalIframe");
or maybe this:
driver.switchTo().activeElement()
It looks the login popup is available inside the iframe tag. First navigate to that particular frame as below and then add the sign in xpath step.
driver.switchTo().frame(0);

Why am I not able to click the web element using the following code, even though I can see that the controller is tracking the xpath?

click here to check the page I am working on
The following is the java code:
driver.findElement(By.xpath(".//*[#id='ref_2665398031']/li[4]/a/span1")).click();
The element is actually in the left navigation pane.
Here when this particular statement is getting executed, I can see that browser is moving down, but it does not click that element.
You have not properly written the xpath:
".//*[#id='ref_2665398031']/li[4]/a/span1"
it should be : "//[#id='ref_2665398031']/li[4]/a/span[1]"
and if using WebDriver then
"//[#id=\"ref_2665398031\"]/li[4]/a/span[1]"
After correcting above if still error exists:
You are using a relative path which is relative to the element having id='ref_2665398031.
This id seems to be generated dynamically.
Please confirm two things:
Your code is not giving element not found error.
Page refresh is not changing this id. (If it is being generated dynamically than it will change on page refresh.)
Otherwise try using different approach:
driver.findElement(By.xpath("//*[contains(text(), '50% Off or more')]"));
Use this xpath:
driver.findElement(By.xpath("//ul[#id='ref_2665398031']/li[4]/a/span[1]")).click();
Try Below xpath it is working for me
driver.findElement(By.xpath("//ul[contains(#id,'ref_2665398031')]/li[4]/a")).click();
Try JavaScriptExecutor instead of using normal Selenium click method.
WebElement element = driver.findElement(By.xpath(".//span[contains(text(),'50% Off or more')]"));
((JavascriptExecutor) driver).executeScript("return arguments[0].click();", element);

How to select options provided in a textbox using Selenium

Link: http://www.bbc.com/weather/
Scenario: Type "reading" on the find a forecast text box. This shows 2 options. How do I select one option using Selenium WebDriver?
I am using the following command to type "reading"
driver.findElement(By.id("locator-form-search")).sendKeys("Reading");
I added explict wait as you suggested and this worked with a small change in the css path. This is what I did:
WebElement suggestedList = new WebDriverWait(driver, 15)
.until(ExpectedConditions.elementToBeClickable(By
.cssSelector("div[class='locator-suggestions locator-suggestions-default'] >ul >li:nth-of-type(1)")));
driver.findElement(
By.cssSelector("div[class='locator-suggestions locator-suggestions-default'] >ul >li:nth-of-type(1)"))
.click();
Use the following locator to click on the first element (first suggestion displayed after typing):
driver.findElement(By.cssSelector("div[class='locator-suggestions locator-suggestions-default'] li:nth-child(1)")).click();
Similarly you can click on the other element by changing the locator. For example to select the second element:
driver.findElement(By.cssSelector("div[class='locator-suggestions locator-suggestions-default'] li:nth-child(2)")).click();
Just to add here, you may need to wait for the elements/suggestions (which you're trying to select) to be visible, before trying to click on those. Use/search for a appropriate wait method for your testing.
So,
first you need to type "reading" in the field as you are doing by using driver.findElement(By.cssSelector("input#locator-form-search")).sendKeys("Reading");
then wait for element to be visible
Then try the above mentioned code to select the desired element.
Hope this helps.

Categories

Resources