So I have basically been stuck on a particular piece of code for a few days now. I am trying to go to a webpage and click a specific toggle button using Htmlunit in Java.
The code I currently have for the java program:
WebClient webClient = new WebClient();
HtmlPage page1 = webClient.getPage("webpage URL");
page1.getElementById("additional_parameters_toggle").click();
The HTML code for the webpage toggle:
<div class="parameters clearfix">
<input type="checkbox" id="additional_parameters">
<label for="additional_parameters class="additional_parameters_toggle" data-name="big old ugly toggle">
<span class = "checkbox_outer">
<span class = "checkbox_inner"></span>
</span>
<span class="label_text">Show details / hourly data</span>
</label>
</div>
I figured that should work but I keep getting a NullPointerException. If there is anyone that could offer some insight or help in anyway, it would be greatly appreciated. Thank you.
As Tom has hinted, .getElementById() will not work since there is no id HTML attribute of that value.
You need to use:
page1.<HtmlElement>getFirstByXPath("//label[#class='additional_parameters_toggle']").click();
which means, find the first element by XPath: search for any label with a class attributes of "additional_parameters_toggle", then .click() it.
page1.getElementById("additional_parameters_toggle")
Looks like you don't have an element having this value as an Id. You do however have an element with an attribute class containing this value.
Related
I am trying to submit the form with selenium api submit(). Click() would not work definitely because it is form button.
Element is "submit".
element.submit() but nothing is happening.
I am trying some java script solution to submit form.
Here is the html:
<form class="ng-pristinedfgd ngrg-valid">
<label class="input-width" for="userName">Username</label>
<input type="text" class="input-width form-input " name="userName" autocomplete="off">
<label class="input-width" for="password">Password</label>
<input type="password" class="input-width form-input " name="password" autocomplete="off">
<p class="forgot-utility">Need login help? Visit the utility website for direction.</p>
<button type="**submit**" class="blue-button">Share Energy Usage</button
</form>
Any help would be much appreciated. Thanks in advance.
As per the HTML you have shared you can invoke click() method as follows:
Java click():
driver.findElement(By.xpath("//button[#class='blue-button'][contains(.,'Share Energy Usage')]")).click();
Note: As the AUT is Angular based so if you are trying to invoke click() soon after Page Load you have to induce WebDriverWait as follows:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//form[#class='ng-pristinedfgd ngrg-valid']//button[#class='blue-button'][contains(.,'Share Energy Usage')]"))).click();
You can always use JS when this happens (it's kind of frequent in some applications) -
driver.execute_script("document.getElementsByClassName('blue-button')[0].click()")
You can call via following:
driver.findElement(By.className("blue-button")).click();
and as #Debanjan mentioned introduce wait.
Thanks everyone for your valuable time. I tried many options but did not work because there was extra margin on that button so Selenium was keep on loosing focus even after intentional focus on element. As I asked developer to remove the extra CSS margin on that button, issue got resolved.
Could someone please assist me in following:
I have button on several languages, which have to click on it. On EN it works correct, while on other languages when doing the same action, it behave as I am holding pressed left button on mouse and moving from left to right through button. It looks like as on screenshot below and HTML is also below.
HTML on DE page:
<div class="column large-12 text-center">
<input id="post-tip-submit" type="submit" class="button secondary expand" value="Veröffentlichen">
<div id="post-tip-loader-9" class="loader-large"><div>Loading...</div> </div><br><br>
</div>
And on EN page:
<div class="column large-12 text-center">
<input id="post-tip-submit" type="submit" class="button secondary expand" value="Publish">
<div id="post-tip-loader-9" class="loader-large"><div>Loading...</div></div><br><br>
</div>
It looks same except 'title' of button, but somehow click on EN button open next page, while click on other language button remains stuck on that page and button disappear (on both languages) after that 'click'.
One more thing: on EN page that submit button is much wider than on rest of languages (not sure how is important last statement).
Tried following code without success:
driver.findElement(By.cssSelector("#post-tip-submit")).isDisplayed();
driver.findElement(By.cssSelector("#post-tip-submit")).sendKeys(Keys.RETURN);
Thread.sleep(7000);
Also, this one:
driver.findElement(By.cssSelector("#post-tip-submit")).isDisplayed();
driver.findElement(By.cssSelector("#post-tip-submit")).click();
Thread.sleep(7000);
XPATH simply does not find that element, CSS does
Please, assist
an id is for an unique element. Try using a class, for example class="post-tip-submit" and cssSelector(".post-tip-submit"). Lets see if it works better?
Css selector is based on your markup hope it helps.
static Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(elementWaitTime, SECONDS)
.pollingEvery(2,SECONDS)
.ignoring(NoSuchElementException.class);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.button[type=submit]")));
element.click()
I'm trying to get selenium to click the select button but I can't use by.linkText() because there are two buttons with the same name.
I'm using this xpath ".//*[contains(#id,'view-something_111111_2A22DF2_)']/div/a[text()='Select']"; to find the button but it can't find it.
I've also tried ".//*[contains(#id,'view-something_111111_2A22DF2_)']/div/a";.
I've looked over the Selenium documentation and can't seem to find a solution.
Here is the section of website code:
<div id="view-something_111111_2A22DF2_0" class="coverage-wrap collapse" aria-expanded="false" style="height: 30px;">...</div>
<div class="btn-raplace">
<a class="btn-beer" data-toggle="collapse" data-target="#view-effectData_111111_2A22DF2_0">Select</a>
for reference, the second Select button has this code:
<div id="view-something_111111_2A3B5DF2_0" class="coverage-wrap collapse" aria-expanded="false" style="height: 30px;">...</div>
<div class="btn-raplace">
<a class="btn-beer" data-toggle="collapse" data-target="#view-effectData_111111_2A3B5DF2_0">Select</a>
which is why I am using the id in my xpath.
Thanks.
You can try this XPATH :- //*[#class="btn-raplace"]/a[#class="btn-beer"][1] here [1] is postion of ur button. Which you want to click
I can see two mistakes in the Xpath you are using.
First Mistake:
.//*[contains(#id,'view-something_111111_2A22DF2_)'] is incorrect.You have placed the single quote at a wrong place. It should be
//div[contains(#id,'view-something_111111_2A22DF2')]
Second Mistake
The element div with the class="btn-raplace" is not the child of the above element. I can see in the HTML that the above element has the closing tags before this element.
Please replace your XPATH with:
//div[contains(#id,'view-something_111111_2A22DF2')]/following-sibling::div[1]/a
Here is the Answer to your Question:
Use this xpath:
//div[#class='btn-raplace']/a[#class='btn-beer']
Let me know if this Answers your Question.
So I am writing automation tests using selenium and I am having a lot of trouble selecting the second element in a list of divs with the same class names
Boolean isExists2Accounts = driver.findElements(By.xpath("(//div[contains(#class, 'item-name')])[2]")).size() < 0;
if(isExists2Accounts)
{
//Finds second div element that has classname of item-name and clicks on it
driver.findElement(By.xpath("(//div[contains(#class, 'item-name')])[2]")).click();
}
else
{
driver.get("javascript:alert('There isn't a second account or you don't know how to select it!');");
Thread.sleep(5000);
org.testng.Assert.fail("transferTest6() Failed due to There isn't a second account or you don't know how to select it!");
}
HTML structure looks like this:
<div class="item-list">
<div class="item-name">
<div> clickable area </div>
<div class="button-wrap"></div>
</div>
<div class="item-name">
<div> clickable area </div>
<div class="button-wrap"></div>
</div>
<div class="item-name">
<div> clickable area</div>
<div class="button-wrap"></div>
</div>
<div class="item-name">
<div> clickable area </div>
<div class="button-wrap"></div>
</div>
</div>
Not really sure what I am doing wrong here, I looked at the html and there are 5 divs with the specified class name. Very new to selenium in general, using eclipse/junit/webdriver.
I have seen several questions similiar to this, and trying solutions people have posted have not worked. I have seen some suggestions to use .get(2) and I will try and implement that in the mean time.
Any help you could give would be good.
get(2) is THIRD element, not the second, as the countage begins from 0.
So:
driver.findElements(By.cssSelector(".item-name")).get(1).click();
OR depending on where is yr clickable
driver.findElements(By.cssSelector(".item-name div:not(.button-wrap)")).get(1).click();
Hey all the answer that was given by Stanjer works, I tested it with different markup, the developer that built the system I am testing through a random mousedown event (not click) for the html I am trying to interact with which was causing the problem.
So final solution with problem if it was a click event would be:
driver.findElements(By.cssSelector(".item-name")).get(1).click();
Just like he said.
However in this case I am instead going to send Javascript to the console to work with functions that have already been created by the developer.
Am a Selenium beginner. have no idea as how to capture tooltip from the following html code.
this is the structure of the html:
<a id="aui_3_4_0_1_2236" title="Graceful shut down of the platform and power-off the hardware.">
<span id="aui_3_4_0_1_2235" class="aui-button">
<span id="aui_3_4_0_1_2234" class="aui-button-`enter code here`content">
<input id="_PlatformSummaryPortlet_WAR_CPFSPGPortlet10SNAPSHOT_INSTANCE_AEDwGJz6R6iD_soft" class="aui-button-input" type="button" value="Soft Shutdown" onclick="javascript:soft()" style="display: inline;"/>
</span>
</span>
</a>
and the title have the tooltip value.
I tried the following to get the tooltip:
WebElement Softshtdwn = driver.findElement(By.xpath(Object.SoftShutdownButton));
String tooltip = Softshtdwn.getAttribute("title");
String tooltip1 = Softshtdwn.getText();
String tooltip2 = Softshtdwn.getCssValue("title");
but for some reason, i get the null value in return.
Any help is appreciated.
note: cannot use by.ID as ID is dynamic.
Using below logic you can get tooltip
But, make sure that you are using proper and unique locator to get exactly required value.
String toolTip=driver.findElement(By.xpath("")).getAttribute("title");
If you are not able to construct proper xpath then post your html code here. I would help you.