Can't find checkbox element by name contains, using Selenium Java - java

I am trying to click on a checkbox that only has a name.
Here is the HTML:
<td class="checkbox-column"><input type="checkbox" name="link-active[138]"></td>
Here is my latest attempt to find the element, I have tried a lot of different methods. I also need to grab it without the "[127]" part since that is dynamic in this case.
driver.findElement(By.xpath("//*[text()[contains(.,'link-active')]]")).click();

You are using wrong xpath, correct xpath is:
driver.findElement(By.xpath("//*[contains(#name,'link-active')]")).click();

You need to check the name attribute.
driver.findElement(By.xpath("//*[contains(#name,'link-active')]")).click();

xpath has a name attribute method, you can try this
driver.findElement(By.xpath("//*[contains(name(),'link-active')]")).click();

You can simply introduce webdriverwait to let your script knows that it is visible and enabled and then you can try to click. Like this :
new WebDriverWait(driver,10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[contains(#name,'link-active')]"))).click();

Related

How can I get the inner text of an element in Selenium?

I'm working with a DOM node:
<input
type="form-control"
type="text"
data-bind="textInput: EnterpriseId"
disabled
autocomplete="off">
How can I get its value? I'm struggling since element.getText() does not work and returns a blank.
Try this:
WebElement element = driver.findElement(By.id("id value"));
String val = element.getAttribute("innerText")
I presume the element in question is an <input> element, so you may be able to use the element.getAttribute(String attribute) method like so:
String value = element.getAttribute("value");
This input tag is disabled, hence element.getText() returns a blank value.
Use element.getAttribute("textContent") instead.
You may be looking for the placeholder of an input text, because you might try:
element.getAttribute("placeholder");
You can go to your browser → open developer tools → inspect element you want to take attribute from → click Properties → check if that value is in InnerText.
Then do as it is mentioned in previous comments:
element_locator.get_attribute('InnerText')
I had the exact same issue! This post solved it for me:
How can I get the current contents of an element in webdriver
I used:
element = driver.find_elements_by_xpath(
'//button[#class="size-grid-dropdown size-grid-button"]')
element.text
As other's suggested, HTML's input nodes don't have a text attribute because they can store data in multiple formats in a value attribute.
This can be easily seen in the HTML input API specification where this form control can be of type radio, date, file upload and many more.
So, in your specific case, I'd suggest you check the webdriver's API for a method that's able to retrieve the value attribute.
As a bonus to evaluate innerText of an element within Selenium:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("yourEl")));
wait.until(ExpectedConditions.attributeToBe(By.id("yourEl"), "innerText", yourValue));
Documentation: attributeToBe
It works definitely, as I've tested it several times:
<input type="form-control" type="text" data-bind="textInput: EnterpriseId" disabled autocomplete="off">
In your example, you don’t have any innerText. So you can only get attributes as mentioned before with the existing attributes. In your case:
type, data-bind, EnterpriseId and autocomplete. No value will be as this attribute isn’t created.
If you want to get only existing, this should be fine:
String example = driver.findElement(ByLocator(("")).getAttribute("any attribute of your input");
System.out.println(example);

How can i click on this button with selenium

How can i click on this button with selenium ?
<a class="_42ft _4jy0 rfloat _ohf _4jy4 _517h _51sy" role="button" href="" ajaxify="/nux/wizard/step/?action=skip" rel="async-post" id="u_9_8">İleri</a>
Something I wish I would have figured out earlier was how to create my own advanced CSS selectors here is the page that taught me, it will work in all cases assuming your element is visible in the DOM.
https://www.smashingmagazine.com/2009/08/taming-advanced-css-selectors/
For your given element you could write this many ways
Generic form
tag[attribute='ATTRIBUTE_VALUE']
For your example
a[id='u_9_8']
or
a[class='_42ft _4jy0 rfloat _ohf _4jy4 _517h _51sy']
or
a[rel='async-post']
Now all these selectors will only be useful if the attribute is unique. But take a look at that article there are many tricks you can use to make CSS selectors work for you.
By using xpath with contains text you can click on the element(below is the answer)
driver.findElement(By.xpath("//a[contains(text(),'Ileri')]")).click();
Try it and let me know if it works for you
Try any of these below mentioned code.
Using id locator
driver.findElement(By.id("u_9_8")).click();
Using xpath locator
driver.findElement(By.xpath("//a[text()= 'İleri']").click();
Explanation:- Use text method along with <a>tag.
driver.findElement(By.xpath("//a[#role='button'][text()= 'İleri']").click();
Explanation:- Use role attribute and text method along with <a> tag.
Please add the wait conditions before you are going to click
Element clicking Via linkText :
webDriver.findElement(By.linkText("İleri")).click();
Element clicking Via id :
webDriver.findElement(By.id("u_9_8")).click();
Element clicking Via cssSelector :
1.webDriver.findElement(By.cssSelector("._42ft._4jy0.rfloat._ohf._4jy4._517h._51sy")).click();
2.webDriver.findElement(By.cssSelector("a[class='_42ft _4jy0 rfloat _ohf _4jy4 _517h _51sy']")).click();
Element clicking Via javaScript :
((JavascriptExecutor) driver).executeScript("arguments[0].click();", webElement);
Here you need to pass the element locator instead of webElement.
We can see ID attribute tag so we can use ID "u_9_8" to click on the button.
use the below code.
driver.findelement(By.id("u_9_8")).click();
I think you should be able to use the id
driver.findElement(By.id("u_9_8")).click();
Give it a shot

can't click on checkbox to uncheck it seem like it's hidden

the checkbox is checked be default and can't click on it to uncheck. here is my code but it came back as error saying element is not currently visible and so may not be interacted with. org.openqa.selenium.ElementNotVisibleException.
String checkboxXPath =("//input[contains(#type='checkbox',#name='key_IT_CONFIG.ios.restriction.functionality.enable.camera_checkboxVal')]");
WebElement elementToClick = driver.findElement(By.xpath(checkboxXPath));
elementToClick.click();
Website code
<input type="checkbox" class="uwp_inputCheckBox"
name="key_IT_CONFIG.ios.restriction.functionality.enable.camera_checkboxVal"
id="key_IT_CONFIG.ios.restriction.functionality.enable.camera"
value="true" dir="ltr" hierarchy="false" expand="true"
checkedval="true" uncheckedval="false"
onclick="checkboxChange('IT_CONFIG.ios.restriction.functionality.enable.camera')"
checked="checked">
whole code
whole code http://imageshack.com/a/img661/1720/SIi6Xj.png
I think you should use explicit wait until element get visible. Please check update code here and use it:
String checkboxXPath =("//input[contains(#type='checkbox',#name='key_IT_CONFIG.ios.restriction.functionality.enable.camera_checkboxVal')]");
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(checkboxXPath)));
WebElement elementToClick = driver.findElement(By.xpath(checkboxXPath));
elementToClick.click();
I have a couple suggestions. I'm not sure why your XPath is so complex when you have an ID on the element you want to click. Try this...
driver.findElement(By.id("key_IT_CONFIG.ios.restriction.functionality.enable.camera"));
I'm kinda guessing that won't work. Looking at the HTML, I see the SPAN right above the element that you want to click and it has an onclick on it. I'm guessing that if you click that, it might trigger the click of the checkbox... so let's try that...
driver.findElement(By.cssSelector("span.uwp_checkBoxSpan.uwp_checkBoxChecked"));
You might need to check my spelling on the class names... I couldn't copy/paste since it's a picture.
Since Selenium works on Javascript I would suggest you to test the checkbox clicking thing manually by entering a Javvascript. Here are the step you need to follow:
Execute the test case manually up till where your script failed.
Goto browsers developer tools option->Console. Enter a javascript
command document.getElementById('key_IT_CONFIG.ios.restriction.functionality.enable.camera').click()
If this works then there is no reason why your code shouldn't work.

Xpath does not work with Selenium

I'm trying to build tests for an old system. The HTML is not well formed. I need to identify and click a radio button.
The html looks like this:
...
<td class="tablerow" colspan="3">
<INPUT type=radio name="ticket" value="22" >ramdom1
<INPUT type=radio name="ticket" value="1" >ramdom2
<INPUT type=radio name="ticket" value="3" >ramdom3
<INPUT type=radio name="ticket" value="99" >ramdom4
</td>
...
I was trying to select the input using xpath as follows:
String xpath = "//input[contains(#name, 'ticket') and contains(#value, '3')]";
WebElement rb = driver.findElement(By.xpath(xpath));
But selenium doesn't found the element.
If change it to
String xpath = "//input[contains(#name, 'ticket')]";
List<WebElement> rbs = driver.findElements(By.xpath(xpath));
or
String xpath = "//input[contains(#value, '3')]";
List<WebElement> rbs = driver.findElements(By.xpath(xpath));
It works, selenium returns a list of elements, including the one that I need. The problem occurs only when I try to use both conditions in a same xpath.
Of course that I could iterate over the list and test each value, but I would like to understand if I'm doing something wrong or not. Since IE doesn´t have native xpath support, I thought this could be a selenium implementation issue.
I'm using Selenium WebDriver (2.37.1) with IE Driver.
Not sure whether this is a Selenium implementation issue but this should work:
"//input[contains(#name, 'ticket')][contains(#value, '3')]"
The use of and is basically the same so the result should be correct here.
I am unsure why that doesn't work, and this technically isn't an answer, but you can replicate precisely what Selenium does to ensure it's not Selenium or any of it's tools at fault.
Selenium uses a library called "Wicked Good XPath" for a Javascript-based implementation of an XPath engine because IE doesn't have a "native" one.
So, to reproduce the scenario, take a copy of your page and add Wicked Good XPath to the script headers. Documentation on the front page of that website is very simple, and very easy to follow.
Once loaded in IE, open the Developer Tools and go into the Console. Wicked Good XPath will need to be "initialised" as such, and therefore you'll need to call wgxpath.install() in the console.
Once done, you now have access to the same library that Selenium would be using. Now, you can call a function within IE's developer console to access the DOM using XPath:
document.evaluate("//input[contains(#name, 'ticket') and contains(#value, '3')]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue
The exact element you need will be returned, at least for me.
Now, admittedly, you don't need XPath at all for this, you can get away with using CSS selectors:
input[name~='ticket'][value='3']
We can use the following css
1.css=input[value='22']
2.css=input[value='1']
3.css=input[value='3']
4.css=input[value='99']
For Checking the Radio Buttons.

Selenium does not find element via xpath using Chromedriver

I'm using Chromedriver to try and find the following element:
<td class="section-menuoption"
onmouseover="this.className='section-menuoption-hilite';"
onmouseout="this.className='section-menuoption';" align="left"
onclick="self.jssDetails.location='products.php?xUser=kery&xRand=123';">
Scheduled Changes
</td>
When using the SeleniumIDE in Firefox I can use this selector without issue:
//*[contains(text(), 'Scheduled Changes')]
However, when I attempt to use this selector with the Chromedriver I get a "no such element" error (running via maven). My Chromedriver code looks likes this:
WebElement box = driver.findElement(By.xpath("//*[contains(text(), 'Scheduled Changes')]"));
System.out.print(box.getText());
box.click();
I've tried different quoting strategies, different xpaths (that also resolve correctly in the SeleniumIDE), but with no success. The only thing I can think of now is changing my XPath implementation, but I'm not even sure that's possible with Chromedriver.
Any help would be very appreciated.
First, make sure it it not in any kind of frames. Otherwise you need switch into the frame first, like this.
Then try use css selector instead of XPath. For example (you may need try different variations):
WebElement box = driver.findElement(By.cssSelector("td[onclick*=\"self.jssDetails.location='products.php?xUser=kery&xRand=123';\"]"));
Also, you might want to post every XPaths you have tried, to help us analyse.

Categories