Using Click() on href Button HTML - java

I am working on a project in selenium, in which once it fills out a textbox, clicks next page, a captcha comes up. First you need to verify, then the captcha shows up. It is just a pop up box in the middle of the page and makes everything else unclickable with a white haze over the page behind it. Once the verification is clicked on I already have a method for solving the captcha.
The issue I am having is that with the methods I have tried, the webdriver cannot seem to find any trace of this button. I always get an no such element exception, when trying to use click(). I have tried wait.until(ExpectedConditions.elementToBeClickable(By.id("home_children_button"))).click();, wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("home_children_button")))).click(); , I have also tried putting a thread.sleep(Num) instead of using a wait, along with many other methods on google. Then when I did some research about href buttons, I found people recommend to use By.linkText. So I tried that with all the methods I tried before, and element still cannot be located. I couldn't find anything else on google, so I thought I would ask for help.
I have attatched the HTML Code I currently am looking at for the button, along with the link for the website. Unfortunately to encounter this page, you must make an account. This page normally comes up once I put the DOB in. Thank you for any clarification provided.
Verify
https://account.battle.net/creation/flow/creation-full
Code I have tried:
wait2.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("fc-iframe-wrap")));
wait2.until(ExpectedConditions.elementToBeClickable(By.id("home_children_button"))).click();
WebDriverWait wait2 = new WebDriverWait(driver, 10);
wait2.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("CaptchaFrame")));
wait2.until(ExpectedConditions.elementToBeClickable(By.id("home_children_button"))).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("home_children_button"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.id("home_children_button"))).click();,
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("home_children_button")))).click();
Thread.sleep(5000) //wait for it to pop up
driver.findElement(By.id("home_children_button"))).click();
Along with trying to use By.linkText.
Solution:
After being told it's an iframe, I looked closer at the HTML to see there was multiple iframes. So I waited to switch through them in order to click the button.
WebDriverWait wait2 = new WebDriverWait(driver, 20);
wait2.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("/html/body/div[4]/iframe")));
wait2.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("/html/body/div/div[1]/div/iframe")));
wait2.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("/html/body/div[1]/div[2]/div[1]/iframe")));
wait2.until(ExpectedConditions.elementToBeClickable(By.id("home_children_button"))).click();

As I suspected, it is in iframe, you need to switch the focus of your driver to interact with verify button :
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("CaptchaFrame")));
wait.until(ExpectedConditions.elementToBeClickable(By.id("home_children_button"))).click();
and once you are done with it, you may want to use defaultContent()
driver.switchTo().defaultContent();

Related

Can we use one pageobject among different pages?

I am working on one selenium project. My scenario is, I have multiple forms one after another. After filling them I have to click on the Next button. I have 4 pages with the next button and they have the same XPath. When I try to use the same web element for the next button it works once but when I try to use that same thing again on a different page It shows element intreactable error.
I would try the following to identify why you get the interactable error (it would help to give us the error at least as is found on the Selenium Documentation)
try to click using javascript executor,
try to add webelements for each action you need to perform (so 4 webelements for each 'Next' button). If it works in this way maybe the webelement is not refreshed after you use it (maybe is a static field). Try also to work with Page Factory pattern.
If nothing works, we should receive more information to be able to help.

Change driver focus between different page in single window in Selenium

I am a beginner in Selenium Automation, and stuck with a situation, where, if I open a page xyz.com, and login, then there are some text box and dropdown and buttons to be clicked but the thing is, after the driver get to that page, I can see those elements, and can get the ID and necessary tags, but these whole page consist of 3 different (.do) page.
For example:
1. main page is abc.do
2. left panel is mno.do
3. center is xyz.do
Image of Inspect Element in IE
and my elements exists in xyz.do, and links in mno.do.
I am using eclipse to code, Selenium 3.1, IE 11
How can I switch between them so that my driver can find the elements, write now its showing element cannot be found, and when I pulled the source code its providing of only first .do page.
Please help.
Thanks.
It seems the page is using frames or iframes, but because you do not provide a URL, it's only a guess.
So, to switch a frame/iframe, simply use the switchTo method:
driver.switchTo().frame0); // Frame by number
driver.switchTo().frame("iframe1"); // Frame by selector
After that call, you switch into the "context" of that page/frame/iframe.

Unable to click on a button which is auto-focused by a script

I'm trying to click a button which is auto-focused by a script in the html.
I've tried both absolute path, relative path but no luck. pretty much anything I can think of. Attached is a screenshot which will display that the xpath is locating the element successfully on the web but when I use it in selenium, it can't find the element.
I've also tried waiting explicitly for 10 seconds for the element using a generic function. (Check the screenshot) This function is working for all other buttons that were called previously except for this one.
Something CAUGHT my eyes is that There's a method being called which auto-focuses the button. I might have to turn the focus to the entire window or the page in this case. I've also tried sending the enter key but still no luck.
Question_1: Does anyone have any solution?
Question_2: Does anyone know how to switch focus to the page?
Question_3: Or anything.
Any suggestion is highly appreciated.
Use Java Script click instead of click. Like this, Hope this will help.
WebElement element = driver.findElement(By.id("SubmitButtonId"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
To switch focus to parent window/default content window you should use switchTo() method on driver instance.
driver.switchTo().parentFrame();
OR
driver.switchTo().defaultContent();
You can use following two statements to know which element is currently focused.
driver.switchTo().activeElement();
driver.getPageSource();
If you try to click on input tag , your script will fail. try to find correct locator only. it will help or try submit() function as you said, this element is auto selected.
I thinks you need to bring element in to view before clicking on the same:
the code for that is:
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true)", webElement);

Can not find selenium WebElement twice

Working with selenium and InternetExplorer 11, I can navigate around this site.
I got a method that finds an input box on the page then inputs text. It works the first time, but when I go to another page it says that element does not exist and the page just refreshes randomly.
I a m just using a simple wait:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(text(), 'Enter Name')]")));
WebElement nameField = driver.findElement(By.xpath("//*[contains(text(), 'Enter Name')]"));
It crashes and says the element can not be found during the wait.
I am not sure if this is just site specific or if I am doing something wrong.
If anyone has any idea I'd appreciate it :)
Here is the Answer to your Question:
As you mentioned when I go to another page the entire HTML DOM gets changed and now if you try to locate or use findElement() with a previously found id/name/css/xpath Selenium will fail to do that.
So when we are on a new page we have to search for the element again using the current id/name/css/xpath in-order to use click() or sendKeys() methods.
Let me know if this Answers your Question.

Selenium Webdriver: IE can't find radio buttons nor checkboxes for using click() on them

I am building a test suite for a basic registration page (it has a large number of text fields + some radio buttons and checkboxes here and there). I am using Selenium Webdriver solution and tests are written in Java. While the tests are running fine on Firefox and Chrome, the Internet Explorer tends to run into trouble when it comes to clicking on radio buttons or checkboxes. All the radio buttons and checkboxes have id-s defined and from what I've learned it's the most convenient way to find an element on the page, so I was quite surprised when I started getting these issues. The method for finding the radio button looks like this:
public static WebElement rad_Male(WebDriver driver) {
element = driver.findElement(By.id("male"));
return element;
}
The click is done in a following way:
rad_Male(driver).click();
As I said, Firefox and Chrome can easily click on checkboxes and radio buttons, but when running tests in IE I get a following exception (the element is visible all the time and I can click on it with mouse):
org.openqa.selenium.ElementNotVisibleException: Cannot click on element
I've also tried using an explicit wait in order to let the elements load before accessing them, but had no luck - I get TimeoutException as soon as the function times out. I suspect it has something to do with the page design, but unfortunately I have no access to the page source code, so I cannot change the page structure to make it easier to test.
The radio button is placed inside a number other divs and I think there is also a table used to align this and other elements around, but this doesn't look too complicated. Here's the code for radio button:
<input type="radio" value="M" name="sex" id="male" tabindex="110">
I think I saw some javascript click suggestion in one of the similar topics, but before resorting to this I wanted to make sure that there is no other way to make it work using the means that Webdriver provides.
I've just started learning Selenium and I try to get my work done on the go while learning new stuff all the time, so I am not too experienced with this yet.
If you would like some more details, please ask as I am not sure if I've got all included.
Thanks in advance!
Hi again and thanks to everyone who responded! A friend of mine had a look at this issue and managed to figure out what was causing this. The radio button was actually contained inside another div in a following way:
<div class="radio" id="uniform-male">
<span>
<input type="radio" value="M" name="sex" id="male" tabindex="110">
</span>
</div>
It appears that this parent div "uniform-male" kind of concealed this button, because Selenium was able to click on this div and as a result, the radio button underneath it was clicked.
I guess I should have posted the code for the radio button along with some code of it's parent elements in the first hand, so it would have been easier to debug it.
Once again I appreciate all the help I received from you on this question, thanks!
try using this before you click on the element, maybe IE is a bit slower:
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(anId)));
As per the error you are getting, I think selenium is trying to click on the element, i.e., radio button, which is probably not visible yet.
To resolve this, try adding an explicit wait in the method rad_Male like this (Assuming 'element' is a reference of 'WebElement class'):
public static WebElement rad_Male(WebDriver driver) {
//waiting 30 seconds for the element to be visible
element = new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOfElementLocated(By.id("male")));
return element;
}
Then, use it to click the button like this:
element = rad_Male(driver); //Fetching the value returned by rad_Male method
if(element!= null)
element.click();
else
System.out.println("Element is not visible");

Categories