Unable to select product name text using XPath - selenium

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')]

Related

how to find xpath of the input box in the following HTML

I have a html like this
<td class="random">
<div id="randomID">Product:</div>
</td>
<td class="random">
<div id="randomID">
<input type="text" class="random">
</div>
</td>
my xpath //div[contains(text(),"Product:")] gives me the first element for which I want to send input. How do I get the input xpath so I can do input.sendkeys() on it.
You use either of the xpath to get the input tag.
Use following and index of input tag
//div[contains(text(),"Product:")]/following::input[1]
Or find the td tag and then use following-sibling
//td[.//div[contains(text(),"Product:")]]/following-sibling::td[1]//input
You can use below xpath as well.
//div[text()="Product:"]/following::input[1]
Or
//td[.//div[text()="Product:"]]/following-sibling::td[1]//input
Use following-sibling
Try with following xpath:
//td[contains(.,"Product:")]//following-sibling::td//input
For the provided code, the input is very simple to identify:
//input[#class='random']
Since I can't see any other duplicate code to force you to be more precise, I don't see the problem in using this one.
If you have multiple , and each or some of them have an input inside, in this case:
//div[text()="Product:"]/following::input[1]
Where Product is the visible text of the div that you want to use as a parent, and then you go straight in the first input taken by its index.

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.

Python Selenium auto-test (How to get text of a column)

Using Xpath tool from chrome dev tools, I have been able to get xpath string and a td object. I 'm just wondering how do we find the value of text in a td object using selenium python web-drivers?
for your case you can use .gettext() method of selenium.
Once I found that, the attribute within td which contains the text is changing and i was supposed to fetch the text in those attribute.
like
<td class="table__cell" data-col-index="1">
<div class="ec-table__cell-content ">813.75</div>
</td>
<td class="table__cell" data-col-index="2">
<span class="ec-table__cell-content ">522.12</span>
</td>
So to get text within all td in a list, I used dynamic Xpath way:
By.xpath("//td[contains(#class,'table__cell')]/following::*").getText()
Try to search by xpath. Relatively to some ID.
Example of:
id("body_content")/table/tbody/tr/td[3]
in this case will be shown exact elements of 3rd column only.

issue accessing a text using xpath

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')]

how to find via style in selenium

I have the following html code:
<tr id="d20209567" style="display: table-row;"><td class="f">7</td><td class="bc">20209567</td><td> </td><td> </td><td class="s">SEÇ</td></tr>
I want to find that tag via a find_element_by_xpath. How do I incorporate style in the following call?
find_elements_by_xpath("//div[#class='bfiltresp']//tbody//tr"
Something like the following didnt work
find_elements_by_xpath("//div[#class='bfiltresp']//tbody//tr[#style='display: table-row']"
Thanks
The following worked
contains(#style,'display: table-row;')
the final call would be:
find_elements_by_xpath("//div[#class='bfiltresp']//tbody//tr[contains(#style,'display: table-row;')]")