Testcafe - How to write testcafe selector to identify element with class contains - testing

I need to write the location of an element in page which has just the tag name and only 1 attribute - class and the class value has number characters which is dynamic so I have to use contains to specify the element.
Could not traverse from parent node as it is a list with similar parent name.
Any Suggestions please??

You can use the Selector.withAttribute method.
For example, the following code finds an input with an attribute, which name ends with 'testId' and clicks on it.
await t.click(Selector('input').withAttribute(/.*testid/);

Related

Problems extracting elements by class names with Selenium

From this page: https://www.realestate.com.kh/buy/, which looks like this in the inspector:
I'm trying to extract all elements of class css-1uuzwjq eq4or9x0 into a list in order to click on the elements and further explore.
I have this code, in which I try to get the elements by their Xpath:
ads = browser.find_elements_by_class_name('css-1uuzwjq eq4or9x0')
for ad in ads:
ad.click()
However, the list always ends up empty. What am I doing wrong?
class attribute holds multiple classes , each class is separated by space. In your case, 'css-1uuzwjq eq4or9x0' are two classes not one
you can find it as :#
xpath
in xpath it validates the class attribute value is exactly the same
//*[#class="css-1uuzwjq eq4or9x0"]
CSS
in css it checks for element that contains both the mentioned class, each class should be mentionedwith a dot
.css-1uuzwjq.eq4or9x0
if you want exact match , use the below locator as it checks the class attribute value to be exact
[class="css-1uuzwjq eq4or9x0"]
using class locator
browser.find_elements_by_class_name('css-1uuzwjq.eq4or9x0')
calss locator uses css locator under the hood , so when you pass a class name, it just adds 'dot' in front of it . so to make it a valid locator pass all classes by replacing space with dot
//div[#class='list']/div[./header[contains(#class,'featured')]]
I would use this xpath to obtain all the divs needed to search.
use /div/header/a if you want the a tag to click.
So it would be
driver.get('https://www.realestate.com.kh/buy/')
hrefs=[x.get_attribute('href') for x in driver.find_elements(By.XPATH,"//div[#class='list']/div/header/a")]
print(hrefs)
for href in hrefs:
driver.get(href)
Would retrieve all the hrefs to loop and will prevent using driver.back() and all the stale elements you will get.
I would not recommend getting by class name because they seem dynamic also those are multiple class names which are grabbed by a css selector.
css-1uuzwjq eq4or9x0 -> .css-1uuzwjq.eq4or9x0

Robot Framework variable attribute in seperate file not storing attribute to be used elsewhere

Variable attribute not getting passed into other file.
I have my variables for element locators stored in one file and I have assertions done in another file which has worked fine until now and separates things out nicely. I am doing an assertion to check that an element exists and its attribute(value) is not blank. If I write it on one page as follows it works perfectly. This use the selenium library keywords should not be equal and Get Element Attribute just to note.
${EXAMPLE} get element attribute class=test test-data
should not be equal ${EXAMPLE} ${EMPTY}
But If I separate them out into different files. So a locators.robot file:
#Locater File
${EXAMPLE} get element attribute class=test[test-data]
And an Assertions.robot:
#Assertion File
should not be equal ${EXAMPLE} ${EMPTY}
It stops working. If I use a selenium library assertion like page should contain element then it works, so I know I am pulling in the other Resource correctly. I have a feeling I may need to store the attribute in another variable somehow and actually assert against that. Any ideas would be great. Thanks in advance.
Suppose you have html code like this as you given in other question -
<div id="top-list">
<div data-version="12345" data-list="1" data-point="10">
Way 1 - Less recommended -
This is how my assertion.robot looks like -
*Settings
Library SeleniumLibrary
Resource Locator.robot
*Test Cases
Test attributes Locator
Open Browser file:///C:Desktop/testxpath.html chrome
${attribute_value}= Get Element attribute ${Datalist_locator_with_all_attribt} data-list
should not be equal ${attribute_value} ${EMPTY}
The locators are in locator.robot file. I'm not calling Get Element Attribute keyword in the locators because doing so there will be no link of directly executing it and referencing back the return value of it in testcase... So just keeping the locators in the locator file nothing else. This locators are accessible when I did Resource Locator.robot in my assertion.robot file. As you can see the Get Element Attribute element take first argument locator of element and second argument is nothing but the attribute name of which value you need. And this keyword returns value of attribute that supplied as second argument. -
*Variables
${Datalist_locator_with_all_attribt} xpath://div[#data-version='12345' and #data-list='1' and #data-point='10']
${locator_with_single_attribute} xpath://div[#data-version='12345']
Output
Way 2 - More Recommended -
Wrap the Get Element Attribute and Should Not Be Equal keywords in one single keyword. and dump it in another keyword file or create *keywords section in locator.robot file itself. Doing this your assertion.robot file will look like this -
*Test Cases
Test attributes Locator
Open Browser file:///C:/Desktop/testxpath.html chrome
Attribute values should not be empty
and locator.robot will look like this. You can make it more generic though -
*** Variables
${Datalist_locator_with_all_attribt} xpath://div[#data-version='12345' and #data-list='1' and #data-point='10']
${locator_with_single_attribute} xpath://div[#data-version='12345']
*** Keywords
Attribute values should not be empty
${attribute_value}= Get Element attribute ${Datalist_locator_with_all_attribt} data-list
should not be equal ${attribute_value} ${EMPTY}
Output

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.

not able to find element using id in findelement in selenium

I am not able to find the element using "id" in selenium as the id is randomly changing in every session of execution so the same id i am not getting in next execution.
As there is no other unique property is there to identify the element.
code sample
You didn't specify a language so I'm going to give you Java. You can do this by using the CSS class or probably a better choice (because of likely uniqueness) is data-lynx-name.
By CSS class
driver.findElement(By.cssSelector("div.http-lynx-json-org-text-input"));
By attribute
driver.findElement(By.cssSelector("div[data-lynx-name='username']"));
You really should read the question that I duped this one to:
Find element by attribute
Also read more about CSS selectors,
http://www.w3.org/TR/selectors/#selectors
You can use XPath.
String xpath = //div[#data-lynx-name='usernameLabel'][text='User ID']/following-sibling::div[1]
The above XPath will will find the div tag containing text 'User ID' and finds the next div which is is the required textbox.
It seems that you can even use the attribute 'data-lynx-name' attribute of the textbox div tag directly.
String xpath = //div[#data-lynx-name='username']
Selenium
driver.findElement(By.xpath(xpath));

How can I focus to a specific item which is in the bottom of the page in IDE

I am trying to select a specific item in a page which is at the bottom of the page. I want to verify that element is present and the same time I want to focus to that specific item.
How can I do this in the Selenium IDE?
I tried storeEval, but its specific co-ordinated which I don't want. I am looking for some dynamic command. I tried using css:.groupTile:contains("Concentrated") but the focus is not going to that particular item (Concentrated).
Can someone help me with Command, Target and value please?
CSS Selectors have many formats
i) Using id. Put this in Target: css=tag#id
tag = the HTML tag of the element being accessed,
id = the ID of the element being accessed
ii) Using class. Put this in Target: css=tag.class
tag = the HTML tag of the element being accessed,
class = the class of the element being accessed
In value you enter name of the item.