Python Selenium Click on all li in ul - html

Hi Ive used the following code and it works perfectly well. How can I make it click all the li items in a ul with an empty ie class="" >With an Xpath of
Xpath is ('//*[#id="Main_data"]/div[1]ul/li[1]/a/span')
html_list = driver.find_element_by_id("myId")
items = html_list.find_elements_by_tag_name("li")
for item in items:
text = item.text
print text
Ive tried item.click in the loop but obtained no results.

To locate elements having an empty class, use the "by XPath" locator:
items = driver.find_elements_by_xpath("//ul[#id = 'myId']//li[not(#class)]")
for item in items:
item.click()

Related

how to fetch first ul element items from nokogiri in rails?

i am having the following html that contains 2 UL elements.
<ul>
1
2
3
<ul>
4
5
6
i am using the following logic to get the data from only first Ul element. but iam getting data from both ul elements. How can i fetch the data only from a first UL element?
uls = xyz_html.css('ul li')
uls.each do |li|
#ul_list_1 << li.text
end
#ul_list_1 displaying all the data as an array like [1, 2,3, 4,5,6] but i need output [1,2,3]
i tried the following logic also but i did not get the output
uls = xyz_html.css('ul:first-child li')
uls.each do |li|
#ul_list_1 << li.text
end
Can some one help me with this
You can use first-of-type CSS selector, which returns first occurence of given element, so:
xyz_html.css('ul:first-of-type li')
More info: https://css-tricks.com/almanac/selectors/f/first-of-type/

Razor - How to store html special character in variable

On my html page, I want to make a list from a collection of entries in my model. However, some of those entries may be null, and in that case I want to have an empty list item, like so:
Something
Something else
More stuff
I do that by inserting a non-breaking space character. (regular whitespace won't generate empty list items) Here's a code snippet:
<ul>
#{
foreach (var x in Model.Entries)
{
var rayon = x.Rayon ?? " ";
<li>#rayon</li>
}
}
</ul>
Sadly, this does not work because it instead pastes verbatim.
And removing the quotation marks, and/or adding #: at the start, won't compile.
What can I do?
You can use Html.Raw.
use like this
<li> #(Html.Raw(rayon)) </li>
Instead of " " just give a blank string:
var rayon = x.Rayon ?? string.Empty;
In fact, I think you can even just scrap that variable assignment together and just use x.Rayon as-is - a null value should have the same effect
<ul>
#{
foreach (var x in Model.Entries)
{
<li>x.Rayon</li>
}
}
</ul>
Update:
Both above work for me. So, if still not working, it looks to be down to CSS/styles on your ul.
e.g.
OK, I think this is down to CSS/styles on the ul that you have then (both above work fine for me). e.g. the following CSS would hide empty li elements:
ul li:empty {
display: none;
}
So, check out your CSS.
If you don't want to change the existing style, you could add an extra class to your CSS and apply that for just this instance.
i.e.
CSS:
ul.show-empty-li li:empty{
display:list-item;
}
HTML:
<ul class="show-empty-li">
...
</ul>

How to create multi level menu using jade.pug

In header.pug
block link
-var selected='Home';
-var menu = [{'title':'Home','address':'/home/home.html','child':[]},{'title':'Shopping','address':'/shopping/shopping.html','child':[{'title':'TV','address':'/tv/tv.html'},{'title':'Smartphone','address':'/smartphone/smartphone.html'}]},{'title':'About','address':'/about/about.html','child':[]}]
#navbar
+navbar(selected,menu)
In mixin.pug
//- Navbar mixin
mixin navbar(selected, menus)
ul
each menu in menus
if selected === menu.title
li.active
a(href=menu.address, title=menu.address)= menu.title
if menu.child.length > 0
+navbar(selected, menu.child)
else
li
a(href=menu.address, title=menu.address)= menu.title
if menu.child.length > 0
+navbar(selected, menu.child)
Problem:
When compile it into html, there is a error, shown in the console:
Cannot read property of undefined in the "if menu.child.length > 0" row
Edit:
Added a picture:
The problem is with the data in menu.child when you call +navbar(selected, menu.child). Objects in menu.child don't have a propety child, so when Pug try to get the length of this undefined values the error appears.
Add child property inside objects in array menu.child.
Or modify a little bit your code to change if menu.child is not undefined (I assume you don't want to, because objects without child property will not be displayed:
if typeof menu.child == typeof []
if menu.child.length > 0
+navbar(selected, menu.child)

Dynamic search/filter core-list (Polymer 0.5)

I need to implement a filter-type search which hides items in core list if they do not match the search. I created a .hidden class that is applied to an item if an expression returns false:
class = {{ {hidden: !match(model.host, hQuery)} | tokenList }}
The elements are hidden, but the list does not reflow elements unless I click on a visible row. Is there a way to force a reflow using a function call?
After a week of struggling, hiding list items is just not the right way to handle this. Iterate through the original array, push any matching objects to a temporary array, and then replace core-list's .data array with the temporary array: this.$.list_id.data = tmpArray. Performance is good for lists up to 10K records.
This is what I'm doing in my code, and it works:
<div style="{{hide_part1}}">
...content to show/hide...
</div>
....
Switching it based on route change(flatron-director):
routeChanged: function(oldValue, newValue) {
if ('some_route_1' == this.route) {
this.hide_part1 = ''
this.hide_part2 = 'display: none;'
} else if ('some_route_2' == this.route) {
this.hide_part1 = 'display: none;'
this.hide_part2 = ''
}
},
Also using core-list's updateSize() and especially scrollToItem(0), i.e. back to top, here and there helps as I also had problems with the 'reflow':
https://stackoverflow.com/questions/29432700/polymer-core-list-is-not-rendering-some-of-the-elements

How to get html tag text using XMLSlurper in Groovy

I am trying to modify html code in Groovy. I parsed it using XMLSlurper. The problem is i need to edit text of certain tag which contains text and children tags. Html code looks like this:
<ul><li>Text to modify<span>more text</span></li></ul>
In groovy i am trying this code:
def ulDOM = new XmlSlurper().parseText(ul);
def elements = ulDOM.li.findAll{
it.text().equals("text i am looking for");
}
The problem is i got empty array in 'elements' because it.text() returns text from 'it' node together with whole DOM subtree text nodes. In this case "Text to modifymore text". Note that contains() method is not enough for my solution.
My question is how to get exact text from a certain tag and not the text from whole DOM subtree?
.text() evaluate children and appends. Hence it will always include merged line.
Could you consinder localText()? Not exactly what you expect, it returns an array of strings.
import org.testng.Assert
ul='''<ul>
<li>Text to modify<span>more text</span>
</li>
</ul> '''
def ulDOM = new XmlSlurper().parseText(ul);
def elements = ulDOM.li.findAll{
String[] text = it.localText();
text[0].equals("Text to modify");
}
Assert.assertTrue(elements.size()==1)