Selenium: css query regarding get_attribute() function - css

I am trying to find the attribute value of the title attribute.
Now, I have a list of similar links on the same page and I want to select the first link and get its title attribute.
I have used the following selenium command:
self.se.get_attribute("css=a[href*='radio?rid=']:nth-of-type(1)#title")
But it is giving me an error.
Could someone please help me figure out the problem?
Thanks

You should use XPath syntax instead of CSS selectors. You didn't post any HTML to match, so therefore a made up example: to get the title of the first link found in a div with id myDiv, use the following:
self.se.get_attribute("xpath=//div[#id='myDiv']//a[1]#title")
Where:
//div[#id='myDiv'] matches any div with id "myDiv";
//a[1] select the first link found anywhere in the previously selected div (use 2 for the second, and so on.
#title specifies the attribute you want to retrieve.

Related

How can I select an XPath with multiple conditions

From this website, I want to get all genres in the Genres & Subgenres menu using the selenium framework. For that, I need a general xpath or css selector that applies to all of them. I have noticed that all genres have "genreid:D+++" as a part of their id and are located in tag. How can I use this information to get all genres? If you know a better way to solve my problem please write it.
https://www.allmovie.com/advanced-search
Xpath for all Genres & Subgenres
//input[contains(#id,'genreid')]
I'm more familiar with CSS than XPath so I will answer that part of your question.
To make a CSS selection for an element ID that starts with genreid, you'd use this selector:
[id^=genreid]
That will give you the checkboxes. If you wanted the <li> elements that contain them, you could just select by the classname: .genre
For more info about CSS selection, see MDN's great documentation here.

css message locator in robotframework

Please advice me about to find css message locator in robotframework.
I can not use id cause of change every time when I run test script.
Only Class not change but have a lot of message.
Hence I want to catch the message instead.
enter image description here
enter image description here
As far as I could understand, you need to select an element based on CSS class. If you need exactly that solution, you can go with CSS locator css:z-menu-img, if you are sure that this is the only element with that CSS class in the page.
Otherwise, I propose another solution (as unstable as the others because it can change over time): use XPath. For your case, it can be: //*[#id="kOIQf6-a"]/span.

Can't display unique react-tag-input tags when mapping data in JSX

I'm having trouble when mapping and rendering multiple react-tag-input components inside my Dashboard component. Each mapped value renders a unique tag, but empty tags are being displayed on all mapped values. Any idea if there's a way to solve this? Here is a codesandbox to help clarify what I mean.
https://codesandbox.io/s/github/luciomcamargo/JSON_UI
Try adding tags. They render on all the mapped values. Also when deleting with backspace all values also delete. Maybe there's some CSS workaround, or a prop I can pass to fix this?
you are keeping all tags in one place so they apply to every item. The easiest solution is to create additional js file for the item with separate tags state inside.
Here is the working solution.

How can I find a link by URL in my functional test?

In the Symfony testing documentation, it shows how to select a link containing specific text:
$crawler->selectLink('Click here');
But the link I'm looking for won't always have the same text. It will say one of a few different things depending on the status of the record, so instead, I need to find it by URL. I think I can figure out how to do it using an xpath filter. Is that what I should go for, or is there a better way?
You would be testing against a known state - and so that state will inform what text should be on the button.
If not - you can also search by a CSS ID, or enough other information to uniquely identify the button, such as a class name of a button within a range specified by an ID, with a CSS Path, or XPath.
Full CSS Path to the `<code>` block in your question
#question > table > tbody > tr:nth-child(1) > td.postcell > div > div.post-text > pre > code
Xpath
//*[#id="question"]/table/tbody/tr[1]/td[2]/div/div[1]/pre/code
A specific ID, or the text to look for would be a lot easier though, and far less likely to break!

How to click on a link based on text in a table using selenium

Hi All,
I have the following table with links that I need to select. In this specific example I need to select the DIY Payroll but sometimes this can change its position within the table. The current xpath is:
.//*[#id='catalog-category-div-1']/table/tbody/tr/td1/ul/li[4]/a
So I do a:
By.xpath(".//*[#id='catalog-category-div-1']/table/tbody/tr/td[1]/ul/li[4]/a").click()
But the problem is here is that it can change position where it can be in td[2] or td[3] and li[n'th postion]
Can I have selenium go through the table and click on it based on text. Will the By.linktext() work here ?
You can use the following codes these codes will handle the dynamic changes.
You can use linkText() method as follows:
driver.findElement(By.linkText("DIY Payroll")).click();
If you want to use xpath then you can use following code.
driver.findElement(By.xpath(.//a[contains(text(),'DIY Payroll')).click();
If you need any more clarification you are welcome :)
I would suggest that you try By.linkText() or By.partialLinkText(). It will locate an A tag that contains the desired text.
driver.findElement(By.linkText("DIY Payroll")).click();
A couple issues you might run into:
The link text may exist more than once on the page. In this case, find an element that's easy to find (e.g. by id) that is a parent of only the link you want and then search from that element.
driver.findElement(By.id("someId")).findElement(By.linkText("DIY Payroll")).click();
The A tag may contain extra spaces, other characters, be capitalized, etc. In these case, you'll just have to try using .partialLinkText() or trial and error.
In some cases I've seen a link that isn't an A tag or contains additional tags inside. In this case, you're going to have to find another method to locate the text like XPath.
You should use a CSS selector for this case:
Can you try:
By.CssSelector("a.browse-catalog-categories-link")
You can use XPath to do this. //a will select all 'a' tags. The part inside of the square brackets will select everything with text "DIY Payroll". Combined together you get the desired solution.
//a[contains(text(),'DIY Payroll')]

Resources