How to verify check box is checked with webdriver.io using element.isSelected() - webdriver-io

Trying to find out if the check box is checked using webdriver.io
XPATH: //*[#id="PageToolbar__i11_btnChkSubmitted"]
using element.isSelected()
Does not work it returns 'false' regardless if the box is checked.

I will rewrite my answer since I now noticed that "checked" is part of value atribute.
You need to first parse that value atribute, that's how you get to "checked" property.
const el = $('#PageToolbar__i11_btnChkSubmitted');
const valueAttr = el.getAttribute('value');
const checkedValue = JSON.parse(value).checked;
I also used just if instead of xPath, it's more readable. Your xPath is not necessary here, plus all you do is use id in that xPath anyway.

Related

Ignore case Xpath #Name attribute c# Selenium/Appium

I have a C# selenium/Appium project where I need to find a desktop Application window By.Xpath("").
This works:
By.XPath("//*[#Name='ASDASD']");
However, some builds of the app have the window name be "ASDasd", which causes the Xpath above to not find the window element and the test fails.
Is it possible to Ignore the case of the #Name attribute whether it be "ASDASD", "ASDasd" or something else?
I did try using the XPath translate function, but I am not able to find the element, I assume I am doing it wrong.
What I tried:
By.XPath("//*[translate(name(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'asdasd']")
or
By.XPath("//*[translate(name(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'asdasd']")
or
By.XPath("//*[#Name='translate(.,'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'asdasd'']")
or
By.XPath("//*[#Name='translate(asdasd,'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')']")
Maybe some other variations too, but I could not get it to work.
Some of the examples may have invalid formatting.
While other seems to be valid but could not find the element and it would timeout.
UPDATE:
Thank you for the assistance, this worked:
By.XPath("//*[translate(#Name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')='asdasd']");
However, it added 60 seconds to the test somehow, it seems to stall for 60 seconds on one of the places where it looks for the main window.
Thanks for the help!
Regards
name() gives you the name of context node. In this case (//*), the name of whatever element you are currently looking at. You meant to write #Name, i.e. the attribute that happens to be called Name.
By.XPath("//*[translate(#Name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'asdasd']")
Using translate() is clunky and fails when the search string contains unanticipated characters.
Unfortunately, there is no lower-case() function in XPath 1.0. But you can work around this limitation with the help of the host language (such as C#).
The following will dynamically create an XPath expression which finds arbitrary values case-insensitively:
var searchValue = "asdasd";
var uc = searchValue.ToUpperInvariant();
var lc = searchValue.ToLowerInvariant();
var xpath = $"//*[translate(#Name, '{uc}', '{lc}') = '{lc}']";
// -> "//*[translate(#Name, 'ASDASD', 'asdasd') = 'asdasd']"

Selecting elements using xpath

So very new here to Selenium but I'm having trouble selecting the element I want from this website. In this case, I got the x_path using Chrome's 'copy XPath tool.' Basically, I'm looking to extract the CID text (in this case 4004) from the website, but my code seems to be unable to do this. Any help would be appreciated!
I have also tried using the CSS selector method as well but it returns the same error.
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.binary_location = '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary'
driver= webdriver.Chrome()
chem_name = "D008294"
url = "https://pubchem.ncbi.nlm.nih.gov/#query=" + chem_name
driver.get(url)
elements = driver.find_elements_by_xpath('//*[#id="collection-results-container"]/div/div/div[2]/ul/li/div/div/div/div[2]/div[2]/div[2]/span/a/span/span')
driver.close()
print(elements.text)
As of now, this is the error I receive: 'list' object has no attribute 'text'
Here is the xpath that you can use.
//span[.='Compound CID']//following-sibling::a/descendant::span[2]
Why your script did not worked: I 2 issues in your code.
elements = driver.find_elements_by_xpath('//*[#id="collection-results-container"]/div/div/div[2]/ul/li/div/div/div/div[2]/div[2]/div[2]/span/a/span/span')
driver.close() # <== don't close the browser until you are done with all your steps on the browser or elements
print(elements.text) # <== you can not get text from list (python will through error here
How to fix it:
CID = driver.find_element_by_xpath("//span[.='Compound CID']//following-sibling::a/descendant::span[2]").text # <== returning the text using find_element (not find_elements)
driver.close()
print(CID) # <== now you can print `CID` though browser closed as the value already stored in variable.
Function driver.find_elements_by_xpath return list of Element. You should loop to get text of each element,
Like this:
for ele in print(elements.text):
print(ele.text)
Or if you want to match first Element, use driver.find_element_by_xpath function instead.
Using xpath provided chrome is always does not work as expected. First you have to know how to write xpath and verify it chrome console.
see these links, which helps you to know about xpaths
https://www.guru99.com/xpath-selenium.html
https://www.w3schools.com/xml/xpath_syntax.asp
In this case, first find the span contains text Compound CID and move to parent span the down to child a/span/span. something like //span[contains(text(),'Compound CID']/parent::span/a/span/span.
And also you need to findelement which return single element and get text from it. If you use findelements then it will return list of elements, so you need to loop and get text from those elements.
xpath: //a[contains(#href, 'compound')]/span[#class='breakword']/span
you can use the "href" as your attribute reference since I noticed that it has unique value for each component.
Example:
href="https://pubchem.ncbi.nlm.nih.gov/substance/53790330"
href="https://pubchem.ncbi.nlm.nih.gov/compound/4004"

How to locate random id generated by a modal?

I was testing my website using RF. The problem is, every time the modal is opened, a different id(locator) will be set on the textbox that I want to input my text. How do you get value of this locator?
I was supposed to try Get Element Attribute but then it cannot support my problem since it still requires a specific locator.
In ROBOT Framework (RF), the locator can be accessed by several ways. Please refer and read this link: http://robotframework.org/Selenium2Library/Selenium2Library.html
The most common way to access the locator is by id such as :
Input Text id:username # Element with id 'username'.
Input Text id:password # Element with id 'password'. you can also use 'Input Password' keyword.
However, if the 'id' element is so dynamic which it keep changing, then the best alternative is to use either ABSOLUTE XPATH expression or CSS selectors. Install the XPATH add-on in your web browser. For firefox, just install ChroPath.
Then, get the ABSOLUTE Xpath element of that username & password text box. Let's assume we know the absolute xpath expression already, so in ROBOT, you can write like below.
${login_absolute_xpath}= Set Variable xpath=/html[1]//div[7]/form[1]/div[1]/input[1]
${password_absolute_xpath}= Set Variable xpath=/html[1]//div[7]/form[1]/div[2]/input[1]
Wait Until Page Contains Element xpath=${login_absolute_xpath}
Input Text xpath=${login_absolute_xpath}
Input Text xpath=${password_absolute_xpath}
...
This should works. Please let me know if this helps.

How to select one from duplicate tag in page in java in selenium webdriver

I am using Selenium WebDriver and I have number of items on a page and each item on page is a separate form type.
I have saved all of these form elements in a list and I am iterating over every item in an attempt to get the name of the element by using the "alt" attribute.
However when I try to get the "name" attribute from the input element it is always returning the first input tag found on that page, not the name attribute of the element I have currently selected.
The syntax I am using is:
((Webdriver imgtags.get(i)).findelement(By.xpath("//input[#name='qty']")).sendKeys ("100");
I have also tried to get the id from the tag by using:
((Webdriver imgtags.get(i)).getAttribute("id");
It's returning a blank value, but it should return the value of the id attribute in that input tag.
I also tried to get the id by using .bytagname but as id is an attribute it is not accessible
Try:
(driver) findElement(By.xpath("//*[contains(local-name(), 'input') and contains(#name, 'qty')]")).sendKeys("100");
To answer the comment by #rrd: to be honest, I have no idea why OP uses ((Webdriver imgtags.get(i)). I don't know what that is. Normally, I just use driver.findElement[...]
Hoping that he knows what works in his framework :D
Selenium Xpath handling is not fully compliant and it does not always treat // as a synonym of descendant-or-self.
Instead try tweaking your code to use the following Xpath:
((Webdriver imgtags.get(i)).findElement(By.xpath("./descendant-or-self::input[#name='qty']")).sendKeys("100");
This will base your search off the currently selected WebElement and then look for any descendants that have a name attribute with a value of "qty".
I would also suggest storing your imgtags array as an array of WebElement e.g.
List<WebElement> imgtags = new ArrayList<>();
This is a much better idea than casting to WebDriver to be able to use .findElement(). This will cause you problems at some point in the future.

How to find whether button is disabled or not in Selenium IDE

I want to check whether button is disabled or not by selenium IDE But I couldn't.
I have tried below code but it doesn't work. is there any other way to find whether button is disabled...? <tr><td>assertElementPresent</td><td>
//button[contains(text(), 'Save')]</td><td>/td></tr>
In WebDriver. There is a method isEnabled which returns true if the element is enabled else it returns false.
driver.findElement(By.id("elementID")).isEnabled();
You can use VerifyNotEditable to check your Element,Button in this case..
A button can be disabled in many ways...so you will need to think about that but a simple solution would be the assertAttribute command, using the attribute disabled.
This will ensure the element has the disabled value set, which is a common way to disable elements, but not the only way.
3 years later...I am using Selenium IDE to test whether the DOM button has been disabled:
Command: assert element present
Target: xpath=//button[#disabled]
now, I have an id for button as well, so I included that in the square bracket to ensure I am "looking" at the right button.
Hopefully, this helps somebody.
I got the answer by following way. I am getting all the style classes by using "window.document.getElementById('requiredId').className" and searching for required disable style class by following expression.
|assertExpression | javascript{storedVars['classname'].search("disabled-style-cl‌​ass") == -1} | false |
instead of is_enable use get_property :
element = driver.find_element_by_name("element_name")
prop = element.get_property('disabled')
You can check the Element visibility by using the assertVisible command.
Code:
Command = assertVisible
Target = Locator Value
Returns true if the specified element is visible, false otherwise
Determines if the specified element is visible. An element can be rendered invisible by setting the CSS "visibility" property to "hidden", or the "display" property to "none", either for the element itself or one if its ancestors. This method will fail if the element is not present.