issue accessing a text using xpath - selenium

I'm trying to find Approvals in the following html and cant seem to get it. I've tried:
//td[*/text()='Approvals']
//td[contains(#class, 'Approvals')]
any help would be appreciated
<td class="ThemeGrayMainItem" name="cmSubMenuID4"
onmouseup="cmItemMouseUp (this,1,'cmSubMenuID4',0,32)"
onmouseout="cmItemMouseOut (this,1,'cmSubMenuID4',0,32)"
onmousedown="cmItemMouseDown (this,1,'cmSubMenuID4',0,32)"
onmouseover="cmItemMouseOverOpenSub (this,1,'cmSubMenuID4',0,32)">Approvals
</td>

You may use contains():
//td[contains(., 'Approvals')]
where . refers to the element's text.
You can also apply additional checks, for instance, on the class name:
//td[#class='ThemeGrayMainItem' and contains(., 'Approvals')]

I would use the following xpath:
//td[contains(text(),'Approvals')]
or if you wanted to be more specific:
//td[#class='ThemeGrayMainItem'][contains(text(),'Approvals')]

Related

Xpath finder for selenium using python -automation

I am trying to find an unique xpath for the below element, please advice if there are any better xpaths to make it for general text as I have currently given for that specific name.
<td tabindex="4" style="text-align: left;" title="name" class="">Name</td>
xpath i am using: //td[#title='name']
here if the name is changed with something else in the code, this wouldn't work, could someone help me identify unique xpath which works in general for any text. Thanks!
You can concatenate (using and / or) multiple attributes of element to find the element precisely .
By.xpath("//td[#title= 'name' and contains(text(), 'Name')]")
However we need to see more details of the code and your DOM of page to find element.
There will always be some element which will never change in the page(like name of table) using that as a relative point ,we can refer to the row of the table.
the simplest way to find the XPath of any element is to go to the developer options and select the markup of the element you want XPath of.
Right Click -> Copy -> XPath
I believe this is the simplest way. And you will also where you are doing wrong.
Screenshot attached for your reference.
I have used the general syntax - "//td[text()='{}']" and passing the name parameter when i define a method so that it won't be specific to one and others can test using the same locator with their name changed when someone else uses the testcase.
Thanks everyone for your response!

How to click on the element through Selenium and Java

I'm trying to click on the button but i cannot focus on it.
<td style="width:100%;height:63px" class="leftNavTabNormal nopad" onclick="selectPerspective('Production')">Production</td>
This is my code:
driver.findElement(By.xpath("//*[#onclick='selectPerspective(Production)']")).click();
I cannot use the "class" because its not uniq
Please help me.
To click on the element with text as Production you can use either of the following solutions:
cssSelector:
driver.findElement(By.cssSelector("td.leftNavTabNormal.nopad[onclick*='Production']")).click();
xpath:
driver.findElement(By.xpath("//td[#class='leftNavTabNormal nopad' and text()='Production']")).click();
Seems like your xpath missed apostrophe
Here is your page code
<td style="width:100%;height:63px" class="leftNavTabNormal nopad" onclick="selectPerspective('Production')">Production</td>
Here is your automation code
driver.findElement(By.xpath("//*[#onclick='selectPerspective(Production)']")).click();
Here is correct code with apostrophe
driver.findElement(By.xpath("//*[#onclick='selectPerspective('Production')']")).click();

How to find an item in Selenium WebDriver?

I want to find the following item using Selenium. The value of the class changes whenever there is a change. This is inside a complex page (multiple iframes, and other items loaded dynamically). The only unique id is itemid, which is dynamic value and title combination. If I click on this Action, am getting another new set of complex items. I am new to Selenium. How to do that?
HTML:
<td itemid="xxyyy.as123" title="Actions" nowrap="" class="text-button">Actions <img src="../row.gif"></td>
<td itemid="xxyyy.as123" title="Actions" nowrap="" class="text-button button-active">Actions <img src="../row.gif"></td>
<td itemid="xxyyy.as123" title="Actions" nowrap="" class="text-button button-hover">Actions <img src="../row.gif"></td>
The code I tried:
Find by Xpath
var element=driver.FindElement(By.XPath("html/body/div[id='pageContent']/iframe/#document/ht‌ml/frameset/frame[name='detailsDisplay']/#document/html/body/form[name='tableForm‌']/div[id='divToolbarContainer']/div[id='divToolbar']/div[1][class='toolbar']/tab‌​le/tbody/tr/td[title='Actions']"));
Find by Link Text
var element = driver.FindElement(By.LinkText("Actions"));
Any help would be appreciated.
Try
By.CssSelector("td[title="Actions"]");
By.CssSelector("td[itemid="xxyyy.as123"]");
By.CssSelector("td[itemid="xxyyy.as123"][title="Actions"]")
Create Dynamic CSS Selector.
For Example:
driver.FindElement(By.CssSelector("td[itemid$="xxyyy."]")).Click();
Note: In dynamic Elements, there is always a part of locator wich is fixed. we need to generate the locator using this part.
If fixed part is at starting - Use Carrot Character (^)
If fixed part is at Middle - Use Asterisk sign (*)
If fixed part is at End - Use Doller sign ($)
Finally I was able to achieve it, by using the frame names.
driver.SwitchTo().Frame("content").SwitchTo().Frame("detailsDisplay");
var element = driver.FindElement(By.XPath("//*[#id=\"divToolbar\"]/div[1]/table/tbody/tr/td[1]"));
Thanks everyone.

Selenium/java-xpath for xlink:href attribute

I tried to locate use element with attribute value '#aaa' for below snippet
<svg>
<use xlink:href='#aaa'></use>
</svg>
I was able to locate using css selector use[* |href='#aaa'] . But I require it in xpath. I tried below xpath but it is not working
(//*[name()='use'])[ * ='#aaa' or #href='#aaa']
Note :I need to use the value '#aaa' to differentiate from other elements.
Could anyone please help to construct this xpath.
Your predicate [ * ='#aaa' or #href='#aaa'] returns false as there is no attribute as href (but xlink:href), and use has no child nodes with text content '#aaa'.
Note that wild-card for any attribute is #* while just * is for any node (except attribute). Try below XPath
"//*[name()='use' and #*='#aaa']"
lookout, if someone needs contains..
"//*[name()='use'][contains(#*='#aaa')]"
as none of the suggestions really worked for me, I came up with following (tested in chrome devtools):
//*[name()='use' and #*[contains(.,'#aaa)]]
comments:
name()='use'
because a normal tag search (i.e //use) did not work
#*[contains(.,'#aaa')]
because contains with a wildcard (i.e. contains(#*,'#aaa')) didn't work
Use the xpath
//use[#xlink:href='#aaa']
or
//use[contains(#xlink:href,'#aaa')]
As the element is within svg namespace you can use the following xpath:
//*[name()="svg"]/use[contains(#xlink:href,'#aaa')]
This is an SVG tag and it works a little differently
Please try below xpath:
//[local-name()='svg']/yourxpath
For XPath:
//*[name()='use'][contains(#*, '#aaa')]
Or variant provided by Andersson
Approaches with [#xlink:href='#aaa'] don't work on practice

Unable to select product name text using XPath

I want to find "Native Starter Pro With Backend" text using XPath. The HTML code looks like this:
<tr>
<td style="text-align:left;vertical-align:middle;border:1px
solid #eee;word-wrap:break-word">
Native Starter Pro With Backend
<br/>
<small>type: Single License</small>
</td>
<td style="text-align:left;vertical-align:middle;border:1px
solid #eee">1</td>
I'm trying to select product name text. I tried this: //tbody/tr/td[1][contains(text(),'Pro With Backend')] but nothing has worked yet.
Have you tried something simple like this?
//td[contains(text(),'Pro With Backend')]
You probably don't need the predecessor tags and I'm not sure why you specified the [1] element when you are looking for some text.
I have tried your HTML code for finding XPath
Use this, it works for me
//td[contains(.,'Pro With Backend')]