Selenium webdriver - How to select dynamic ID's using xpath? - html

Using xpath,I need to select text "level38a" using dynamic id "select" link. In HTML code i see containing 2 <td> with text "level38a" and "select".
PS : In my application i have more than 30 "select" link for different text. So using the id="lnk_LEVEL_2449" is not efficient.
I'm using the below code to select the text, but it's not clicking the select button.
driver.findElement(By.xpath("//tr[td//a[#value='Select']]/td/a[contains(text(),'level38a')]"));`
HTML snippet
<tr>
<td>
<img class="imHeader" alt="" src="include/img/context/level_dash.gif">
<img class="imHeader" alt="" src="include/img/context/icon_telco_level.gif">
level38a
</td>
<td> </td>
<td>
<a id="lnk_LEVEL_2449" href="jfn?isLevel=true&level=L4%3A2449&mfunc=614&cfunc=615&oid=L4%3A2191&ctx=L&jfnRC=9">Select</a>
</td>
</tr>

This method
driver.findElement(By.xpath("//tr[td//a[#value='Select']]/td/a[contains(text(),'level38a')]")); only finds the element, doesn't click it.
You have to add click() action. Like this:
driver.findElement(By.xpath("//tr[td//a[#value='Select']]/td/a[contains(text(),'level38a')]")).click();
Edit:
I haven't looked at your xpath. It is wrong.
Please try these:
"//tr[td//a[contains(text(), 'Select')]]/td[contains(text(),'level38a')]"
or just:
"//td[contains(text(), 'level38a')]"

Used the below xpath to select dynamic ID's and it worked fine. First it locate the text "level38a" in the page and click on "Select" link.
driver.findElement(By.xpath("//tr[td[contains(text(),'level38a')]]/td//a[contains(text(), 'Select')]")).click();

Related

Find superior element in html with Selenium

I have the following html code:
<tr id="id that I want to obtain via title">
<td class="icon">
<span class="greenArrowIcon pid-1-arrowSmall"></span>
</td>
<td class="bold left noWrap elp">
<a href="..." title="title that I have obtained">
TITLE
</a>
<MORE CODE>
</td>
</tr>
and I know that the tag title title="title that I have obtained" are always the same, but the id id="id that I want to obtain viva title" could change, is strange that changes, but could.
So, my question is: How can I find the id via the title ? I think the problem is that the title tag is inside (an inferior jerarchy) from the id that I want to solve it.
I am using Selenium, and this is the code to solve the title and get the web element:
driver.find_element_by_css_selector("[title^='title that I have obtained']")
Is it possible do this?
To find the desired id attribute value you can use the following XPath locator:
tr = driver.find_element_by_xpath("//tr[.//a[#title='title that I have obtained']]")
id = tr.get_attribute("id")
Already #Prophet answered in a good way but I want to show you other ways to find the same element.
Using parent function:
//a[#title='title that I have obtained']//parent::td//..
//a[#title='title that I have obtained']//..//parent::tr
Using ancestor function:
//a[#title='title that I have obtained']//ancestor::tr

Robot Framework: How to get xpath of an element?

I am trying to retrieve the id of a parent (grand-grand-parent) element.
All the information I have is that //div[#class="preset_name"] is "all". Knowing that, I need to retrieve "preset_3" (as a string) using any of the Robot Framework keywords.
How would I do that? Or how can I obtain the full xpath of "all"?
<table id="preset_list">
<tbody>
<tr id="preset_0">
<td>
<div class="preset_title_line">
<div class="preset_button_group">
<button class="preset_button">Apply</button>
</div>
<div class="preset_name">Factory default</div>
</div>
<div class="preset_sections">System, Network, Sources, EDID, Channels, Automatic File Upload, Touch screen, Output ports</div>
</td>
</tr>
<tr id="preset_1">
<td>
...
You can filter tr that contains //div[#class="preset_name"] = "all", and then return the corresponding id attribute :
//tr[.//div[#class='present_name'] = 'all']/#id
Your XPath should be:
Get Element Attribute xpath=(//div[#class="preset_name"][text()='all']/../../..)#id
(I've never used Robot, but I think it is the correct sintax)
Its easy if you are using "relative xpath helper chrome extension".
https://www.linkedin.com/pulse/finding-relative-xpath-made-easy-syam-sasi?trk=prof-post

Extract information from web page throughExcel vba

I have the following html code
<tr>
<td class="fontblue" style="height: 17px">
Primary Industry Code
</td>
<td class="fontlightblue" style="height: 17px" colspan="4">
<span id="ucOrganisationDetail_lblVPrimarySector">
FMP CB:Miscellaneous Manufacturing
</span>
</td>
</tr>
I would like to extract the text FMP-CB Miscellaneous Manufacturing using getelementbyID option in vba. can anyone please help me
I tried using the below vba code but nothing is working
objIE.Document.getElementByID("ucOrganisationDetail_lblVPrimarySector").Value
Try the following:
objIE.Document.getElementByID("ucOrganisationDetail_lblVPrimarySector").innerHTML
You want .innerText as mentioned in comments I see.
.innerText
Sets or returns a String that represents the text between the start
and end tags of a specified object without any associated HTML.
expression.innerText
expression Required. An expression that returns one of the objects
in the Applies To list.
You could use
objIE.Document.getElementById("ucOrganisationDetail_lblVPrimarySector").innerText
or a CSS selector which says the same thing of:
objIE.Document.querySelector("#ucOrganisationDetail_lblVPrimarySector").innerText
The element is correctly selected as you can see with:

using xpath axes correctly to locate

I am testing a web page with dynamic ids. The page contains a remove icon followed by a browse icon at multiple places and I am not able to click these icons or I must say I am not able to write the correct XPath to locate any particular icon.
The following is a snippet of the HTML code.
<tr class="tableNestedAttribute">
<td class="autosuggest-svl-cell">
<input name="odf_obs_fin_dept_text">
<div id="div_d31505e760_location"></div>
</td>
<td class="actions" nowrap="" align="right">
<div class="ui-browse-lookup-images ppm_field formFieldDisNoWidthReadOnly ">
<img id="d31505e7601" title="Remove Department OBS" name="Remove">
<img id="d31505e7600" title="Browse Department OBS" name="Browse">
</div>
</td>
</tr>
The above HTML code is at multiple places in the page except that the name in the first td is different at each place.
My requirement is here to click the browse which is in the same line as the input field with name odf_obs_fin_dept_text.
Tried the following and various other combinations but with no luck.
driver.findElement(By.xpath("//*[#name='odf_obs_fin_dept_text']/following-sibling::td[1]/descendant::div/img[#name='Browse']")).click();
With the provided example the following XPath:
//input[#name='odf_obs_fin_dept_text']/parent::td/following-sibling::td[1]/div/img[#name='Browse']
will find the next element:
<img id="d31505e7600" title="Browse Department OBS" name="Browse">
Or if you want to keep your XPath intact, only add parent::td between //*[#name='odf_obs_fin_dept_text'] and following-sibling::td[1]
//*[#name='odf_obs_fin_dept_text']/parent::td/following-sibling::td[1]/descendant::div/img[#name='Browse']

How to edit HTML in Vim?

I'm new to Vim and I'm trying to get used to it. I just created a .vimrc file and got Vim to display line numbers and do incremental searching. I also enabled syntax highlighting. Now I want to enable things to make writing HTML easier. I searched for html.vim in /usr/share/vim and found this:
/usr/share/vim/vim72/syntax/html.vim
/usr/share/vim/vim72/ftplugin/html.vim
/usr/share/vim/vim72/indent/html.vim
Now, what do I have to do to enable HTML auto indentation? Copy those files to ~/.vim? Symlink them? Or does Vim automagically load them from /usr/share/vim/? (It already does HTML syntax highlighting, so I think that's possible - but it doesn't do HTML auto indenting)
I heard set autoindent in .vimrc would do the trick, but what's with .c files? I thought they needed set cindent, but does cindent work with HTML?
The very first thing you should do is try vimtutor and complete it a couple of times. Once the basics are covered you can start to play with plugins…
SnipMate is inspired by TextMate's snippets and/so is beautiful, it has a lot of HTML snippets by default and it's extremely easy to add your own. To use it, type div then hit Tab to obtain:
<div id="|">
</div>
with the caret between the "" ready for you to type an id; hit Tab again to move the caret on the blank line:
<div id="myId">
|
</div>
Beautiful. Many editors have this feature, though.
If you have a lot of HTML to write — say a few emails/newsletters a day — another plugin called SparkUp allows you to produce complex HTML with only a few key strokes and some CSS knowledge. You start by typing something like:
table[id=myTable] > tr*3 > td*2 > img
then you hit <C-e> (CtrlE) to obtain:
<table cellspacing="0" id="myTable">
<tr>
<td>
<img src="|" alt="" />
</td>
<td>
<img src="" alt="" />
</td>
</tr>
<tr>
<td>
<img src="" alt="" />
</td>
<td>
<img src="" alt="" />
</td>
</tr>
<tr>
<td>
<img src="" alt="" />
</td>
<td>
<img src="" alt="" />
</td>
</tr>
</table>
with the caret inside the first empty "". Hit <C-n> and <C-p> to go to the next/previous field.
Magical. The plugin is available for more editors, though.
I second text objects and Surround.vim which are unbelievably useful.
Another cool feature is the visual-block mode (:help visual-block) where you can select columns of text. Say you have:
<ul>
<li><p>My text doesn't mean anything</p></li>
<li><p>My text doesn't mean anything</p></li>
<li><p>My text doesn't mean anything</p></li>
<li><p>My text doesn't mean anything</p></li>
</ul>
place your cursor on the > of the first <li>then hit <C-v>and move the cursor downward to the fourth <li>. Hit I (capital I) to enter INSERT mode just before the > and type class="myElement" then <Esc> to obtain:
<ul>
<li class="myElement"><p>My text doesn't mean anything</p></li>
<li class="myElement"><p>My text doesn't mean anything</p></li>
<li class="myElement"><p>My text doesn't mean anything</p></li>
<li class="myElement"><p>My text doesn't mean anything</p></li>
</ul>
Ho yeah!
Seriously, Vim is great.
Take a look at the AutoCloseTag plugin to close tags as you type them. And set autoindent should be handling HTML indentation for you.
Also you should read the docs in :help text-objects to learn about using the inner and outer tag selections. For example, in normal mode you can do cit to change the text inside the current tag. Or in visual mode at will expand the visual selection to encapsulate the tag around the cursor.
Finally, look at the Surround.vim plugin, which can surround a selection or text object with a tag, or change the tag around it.