How to create xpath to select book? - selenium

Here I am struggling to finding xpath to select the text Book from below Images for Web content and DOM
Advice me how to select book text.
I have done up to this point. May be its strait forward
Xpath I created is :
//*[#class='four columns m-r-3']//following-sibling::h2/strong
image of web content

Try
//h2[#class='font-plus-2 m-b-half']/strong/text()
UPDATE:
Looks there is space in the value for element strong, so you may normalize it as shown below:
//h2[#class='font-plus-2 m-b-half']/strong[normalize-space(.) = 'Book']

Related

How to identify element by text when text is spanned over multiple lines using Selenium

Is it possible to write xpath using contains text such as(Below is what I want but does not work)
//ul[#role='listbox']/..//span[contains(text(),'Fast-Food Restaurent')]
Page Code:
<span class="item-title" md-highlight-text="searchText" md-highlight-flags="i">Fast-<span class="highlight">Food</span> Restaurant</span>
It is an auto complete text box when I enter the word food, there are some options and I want to select Fast-Food Restaurant from it.
Thanks in advance.
To identify the element you can use either of the following xpath based Locator Strategies:
Using the texts Food and Fast:
//ul[#role='listbox']/..//span[.//span[text()='Food']][contains(.,'Fast-')]
Using the texts Food and Restaurant:
//ul[#role='listbox']/..//span[.//span[text()='Food']][contains(.,'Restaurant')]
Using the texts Food, Fast and Restaurant:
//ul[#role='listbox']/..//span[.//span[text()='Food']][contains(.,'Fast-') and contains(.,'Restaurant')]
You can select required span node based on its string representation:
//span[.='Fast-Food Restaurant']
You can amend your path to the following:
//ul[#role='listbox']/..//span[contains(normalize-space(), 'Fast-Food Restaurant')]

Identifying an element from a group, span[i] is the differentiating factor

I have added the screenshot I have a group of elements that have the exact same xpath except the span tag.I want to identify the individual input fields, but unable to.
I have tried using contains, with class but unable to attach span to the xpath
Here is what the HTML looks like:
/html/body/div[#id='app']/div/div[#class='LayoutModify_LayoutModify_1Akxb']/main[#class='LayoutModify_main_5aBy3']/section[#class='sub-detail inner ProductDetail_productdetail_bJWN2']/div[#class='ProductDetail_productsphere_kgNGm']/div[#class='ProductDetail_threecol_2zA1n ProductDetail_productsphereleft_2pLZT']/span[4]/div[#class='el-input el-input--medium ProductDetail_productsphereinput_3eVZg']/input[#class='el-input__inner']
/html/body/div[#id='app']/div/div[#class='LayoutModify_LayoutModify_1Akxb']/main[#class='LayoutModify_main_5aBy3']/section[#class='sub-detail inner ProductDetail_productdetail_bJWN2']/div[#class='ProductDetail_productsphere_kgNGm']/div[#class='ProductDetail_threecol_2zA1n ProductDetail_productsphereright_3BrqC']/span[4]/div[#class='el-input el-input--medium ProductDetail_productsphereinput_3eVZg']/input[#class='el-input__inner']
Notice the span[4] and span[15] are the only differences
Quick question:
do either of these locators:
locator A
//span[4]/div/input[#class='el-input__inner']
locator B
//span[15]/div/input[#class='el-input__inner']
find any input on Your page?
If not - could You please post here the whole HTML page code here please?
Here is the xpath that worked:
//div[contains(#class,'ProductDetail_productsphereright')]/span[4]/div/input

How to Identify Elements in Salesforce Lightning for Selenium

I am trying to automate Salesforce lightning using Selenium, but getting issues with identifying elements. Reason, its having dynamic IDs , and other attributes are either very long , or they are not unique.
For eg ,
<a id="170:1968;a" class="textUnderline outputLookupLink slds-truncate forceOutputLookup"
data-refid="recordId"
data-recordid="0059E000001aOCSQA2"
data-special-link="true"
href="#/sObject/0059E000001aOCSQA2/view"
target="_blank" rel="noreferrer"
title="" data-aura-rendered-by="170:1968;a" data-aura-class="forceOutputLookup"/>
In above code , ID is dynamic , Class is not unique, and all the Lookup elements are associated with it. Also the absolute path is not much trusted , and hence I am trying to find any concrete option to handle these elements. Any help will be highly appreciated.
Here, you could try using the contains method if at least a part of the id attribute value is static.
From your code, you could try
//a[contains(#id,"a")]/ //--extended xpath--
From the given html code, the 'a' in the id attribute of the a tag looks static, while the rest changes.
You can ask the developers to provide an id to the lightning component using aura:id
Then the dynamic id won't be generated.
You can try with field labels and fetch its parent node(s), and then fetching childs or brother nodes to locate related texts/text boxes etc.
Eg. You are in Account Edit/New page, and you want to fill in a value to the text box for Account Name field. So you can firstly try with //*[text()='Account Name']/parent::* to find an element that covers BOTH the field label and the text box.
And then you can check if the text box is a 'brother' or a 'child'. If it's a 'child' then try with //*[text()='Account Name']/parent::*(/parent::*)//*[attributes for the text box];
If it's 'brothers' then try with //*[text()='Account Name']/parent::*(/parent::*)/following-sibling::*[attributes for the text box]
You can use this logic to locate all type of fields in all standard lightning pages.

Selecting a button/label in selenium web driver to display more text

So in the webpage I am trying to write some selenium for, there is a label that causes more text to be displayed. I am trying to figure out how to have selenium select the label and then have the rest of the text displayed.
The button/label I am trying to get it to select is <label class="btn_expand" for="kpanel">Show Full Display</label>
When I tried using select I got an error "Element should have been select, but
instead was label". This was the code I tried -
Select select = new Select(driver.findElement(By.xpath("//label[#class=' btn_expand']")));
select.deselectAll();
select.selectByVisibleText("Show Full Display");
Thanks in advance.
I actually fixed this problem by using:-
driver.findElement(By.xpath("id(//label[text() = 'Show Full Display']/#for)")).click();
Read this to learn what a SELECT HTML tag is. You don't want a SELECT tag, you want the LABEL tag.
Your XPath is not efficient... it contains more than is actually needed to find that element. XPath, in general, is error prone, fragile, and slow. I prefer CSS Selectors. Try this
driver.findElement(By.cssSelector("label[for='kpanel']")).click();

How to select a specific table cell using HTML Agility Pack

I have to pull out particular fields from cells in an HTML table. Using Firebug I was able to get the exact XPath to the cells I need (unfortunately, the cells don't have an id tag). I thought I could use DocumentNode.SelectSingleNode and pass in that path, but it doesn't seem to be working right. What am I doing wrong? Or is there a better approach to this than how I am doing it? Unfortunately, I have no experience with XPath so this is turning out harder than I expected it to be. Here's what I have so far (I know the HTML is particuarly messy, but that's not in my control to change):
Dim page As New HtmlAgilityPack.HtmlDocument
Dim node As HtmlAgilityPack.HtmlNode
page.LoadHtml(fileContents)
node = page.DocumentNode.SelectSingleNode("/html/body/form/div[6]/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr/td[2]")
Much appreciated.
Firebug maybe fixed broken html tags.
If you want to pick and Html node,it is recommend use class or id.
For example:
//div[#class='content']//table//tr[1]/td[2]
shorten the path,and use class or id selector.
if the table has it's own id,you can use:
//table[#id='tableid']/tr[1]/td[2]
try it,you will find XPATH is interesting.