how to find via style in selenium - 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;')]")

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.

Unable to find correct Target selector to click an element

Please see the image below which shows code of the web page in Developer Tools window and advise me the correct selector for 'Depreciation Models'. Text highlighted in red is constant, however the surrounding text is dynamic. So I tried using contains selector to locate but unsuccessful. I want to avoid XPATH as number of before and after div elements may keep changing.
I am using Selenium IDE hence the C#/Java code for RC/Webdriver won't help much.
Selenium IDE generated target path is this:
css=#dhxId_rgWATog7lC3E_27572|6059|6152 > td.sub_item_text > div.sub_item_text
I tried
css=contains('27572') > td.sub_item_text > div.sub_item_text
but it didn't work.
Kindly suggest. I am stuck. Thanks.
As you tried the Locator Strategy as :
css=contains('27572') > td.sub_item_text > div.sub_item_text
Reasons for not working
From the snapshot of the HTML you have provided, 27572 is not the innerText but partial string of the id attribute.
As per selenium.common.exceptions.InvalidSelectorException with “span:contains('string')”:
The :contains pseudo-class isn't in the CSS Spec and is not supported by either Firefox or Chrome (even outside WebDriver).
Solution
You can use the following xpath as per the existing DOM Tree:
xpath=//tr[#class='sub_item'][contains(#id,'27572')]//td[#class='sub_item_text']/div[#class='sub_item_text'][contains(.,'Depreciations Models')]
I guess this would be the right approach:
<style>
[id*="27572"] > td.sub_item_text > div.sub_item_text{
color:red;
}
<table>
<tbody>
<tr id="dhxId_rgWATog7lC3E_27572|6059|6152" class="sub_item">
<td class="sub_item_icon">
<i class="fa fa-user epc-down-circled-2"></i>
</td>
<td class="sub_item_text">
<div class="sub_item_text"> Depriciations Models</div>
</td>
</tr>
</tbody>
</table>
It will set all elements that have an id attribute value containing "27572" and inside of it.

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

Clicking on checkbox based on span value in WebDriver

I Would like to select the the checkbox based on "Costco Wholesale Corporation" value in:
<td role="gridcell" style="" title="" aria-describedby="entity-search-grid_selected">
<input type="checkbox" name="chkbox" class="itmchk" value="110504">
</td>
<td role="gridcell" style="font-weight: bold;" title="Costco Wholesale Corporation" aria-describedby="entity-search-grid_name">
<div class="tree-wrap tree-wrap-ltr" style="width:18px;">
<div style="left:0px;" class="ui-icon ui-icon-triangle-1-s tree-minus treeclick"></div>
</div>
<span class="cell-wrapper">Costco Wholesale Corporation</span>
</td>
I have tried the following code:
driver.findElement(By.xpath("//td[span[text()='Costco Wholesale Corporation']]/preceding-sibling::td/input[#name='chkbox'"));
but getting following exception:
invalid selector: Unable to locate an element with the xpath expression //td[span[text()='Costco Wholesale Corporation']]/preceding-sibling::td/input[#name='chkbox' because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//td[span[text()='Costco Wholesale Corporation']]/preceding-sibling::td/input[#name='chkbox''
Could you help to write the xpath selector.
Your first problem is that xpath td[span[text() is definitely invalid. Remember that the [...] syntax is only for filters, not for defining the path. So it has to be:
//td/span[text...
Also since you select span first, you need to get back to td level and then choose a sibling:
//td/span[text()='Costco Wholesale Corporation']/../preceding-sibling::td/input[#name='chkbox']
Given your HTML, you could also simplify it to select td byt title and then its sibling:
//td[#title='Costco Wholesale Corporation']/preceding-sibling::td/input[#name='chkbox']
I wonder why you want to use xpath. The moment someone changes the structure of the site the whole selector will be worthless. I really recommend sticking to css selectors as long as you can. In this case it's just:
driver.findElement(By.css("input[name='chkbox'][value='110504']"));
And the main advantage of this approach is that only changes which directly concern the element will force you to change the selector.
Of course my selector might not be valid if there are some parts of site that you didn't show us and that may have inpact on selector. But even then I believe there is a way to use css to get to the element more directly.
Your provided xPath is syntactically incorrect.. try using below xPath :-
String xPath = "//span[text()='Costco Wholesale Corporation']/preceding::input[#name='chkbox']";
driver.findElement(By.xpath(xPath));
Hope it helps...:)

How to click the image link by selenium webdriver?

I have the following html page source, and I tried to click the image by xpath/cssselector. none can work, could you help me to find the issue with my code? I use IE9.
<pretable border="0" cellpadding="0" cellspacing="0" width="700">
<tr>
<td rowspan="2" width="120">
<a href="#" onclick="oCMenu.m['top1'].b.moveIt(8,60); oCMenu.showsub('top1'); "
onclick="return false" class="FontNormal">
<img border="0" src="images/shim.gif" width="112" height="73"></a></td>
</tr>
</pretable>
my code is:
ieDriver.findElement(By.xpath("//html/table/tr[1]/td/a[#class='FontNormal']/img[#src='images
/shim.gif']")).click();
ieDriver.findElement(By.cssselector("class='FontNormal'")).click();
With the available DOM Structure we can always go with CSS Selector.
CSS Selectors
css=a[href='images/shim.gif']
css=a[href*='shim.gif']
Then Perform
driver.findElement(By.cssSelector("a[href='images/shim.gif']")).click();
OR
driver.findElement(By.cssSelector("a[href*='shim.gif']")).click();
You are trying to provide a full xpath and yet it does not match up with your html provided.
'table' is not the same as 'pretable', and you don't need to supply the full path anyway. Instead just try this for your xpath:
XPath("//a[#class='FontNormal']")
Here, xpath will search for any link with the class 'FontNormal' attached. If there is only one, this will select your element. If there is more you may need to be more specific.
First write proper xpath to identify that webelement where you want to perform click operation
driver.findElement(By.Xpath("//img[# src='images/shim.gif']")).click();
element(By.xpath('//html/table/tr[1]/td/a[#class='FontNormal']/img[#src='images
/shim.gif']')).click();
Use Firebug with Firepath for Mozilla Firefox. This will automatically generate xpath for you.
Webelement element = driver.findElement(By.xpath("*xpath here*");
element.click();
click on image link by using xpath.
driver.findElement(By.xpath("//img[contains(#src,'/static/images/image_name.png')]")).click();
You can change the image path depending on your image source.