xpath : get 2nd href link to last - xpath

I want to know how to get href link from position 2nd to last,
This is my xpath :
date = sel.xpath("//div[#class='date']//ul/li/a/#href").extract()
And it get result like :
[u'http://www.job.com/?date=12/15/2014', u'hhttp://www.job.com/?date=12/16/2014', u'http://www.job.com/?date=12/17/2014', u'http://www.job.com/?date=12/18/2014']
And I don't want the first one http://www.job.com/?date=12/15/2014
I want to choose li[2] to the last li
How can I do this?
Please guide me
Thank you.

Try this :
date = sel.xpath("//div[#class='date']//ul/li[position() >= 2]/a/#href").extract()

Related

Unable to locate element by giving variable name inside XPATH

brand_name = 'LACOSTE'
element = browser.find_element_by_xpath("//*[text()= brand_name]")
This is giving error. However, if i do like this:
element = browser.find_element_by_xpath("//*[text()= 'LACOSTE']")
then element is found.
But i dont want to hardcode the value. Please help.enter image description here
The XPath needs apostrophe's around the search-string, so I think you need to concatenate the XPath expression like this
element = browser.find_element_by_xpath("//*[text()='" + brand_name + "']")

How to take xpath to Get Text from class inside th

I have the following XPath :
//table[#class='ui-jqgrid-htable']/thead/tr/th//text()
And I'm trying to get the text from it with the following command :
String LabelName = driver.findElement(By.xpath("//table[#class='ui-jqgrid htable']/thead/tr/th//text()")).getText()
But it's not printing text, the result is blank. Could you help me please ?
The text() in your xpath does not qualify as an element. Your element ends at //table[#class='ui-jqgrid-htable']/thead/tr/th. Try using getText() for this XPath.
Also, a table would have many headers. Using findElement will only return the first one.
If you want to get all headers use
driver.findElements(By.xpath("//table[#class='ui-jqgrid-htable']/thead/tr/th"))
and loop through the list to getText of individual element.

how can i find xpath or CSS selector for only ''Total Cases'' in the following table (url =this https://www.worldometers.info/coronavirus/ )

I tried several types of XPath but none of those working as I want to
Xpath : //td[#class='sorting_1'],
xpath: //tr[contains(#class,'even')]//td[#class='sorting_1'],
xpath : //tr[contains(#class,'odd')]//td[#class='sorting_1']
CSS: .even+ .odd .sorting_1 , .even .sorting_1
but the CSS selector does not work in the scrappy shell
can you please help me out of this situation??
To get total cases by country, just use :
//table[#id='main_table_countries_today']//td[contains(#style,'text-align:left;')][normalize-space()]/following-sibling::td[1]
To get the country names :
//table[#id='main_table_countries_today']//td[contains(#style,'text-align:left;')][normalize-space()]
Output (219 lines):
Side note : normalize-space is used to filter "the ghost" line present in the table (no country name and a value of 721). Probably a leftover of and old "Diamond Princess" record.
EDIT : In fact 721 corresponds to the total number of cases on the two ships ( Diamond Princess and MS Zaandam)
EDIT : If you want to get the data for each country (ships and World included) located on the first tab only (213 nodes) :
//table[#id='main_table_countries_today']//td[contains(#style,'text-align:left;')][parent::tr[not(#style="display: none")]]/following-sibling::td[1]
To exclude the ships (211 nodes) :
//table[#id='main_table_countries_today']//td[contains(#style,'text-align:left;')][parent::tr[not(#style="display: none")]][not(./span)]/following-sibling::td[1]
To exclude ships and World (210 nodes) :
//table[#id='main_table_countries_today']//td[contains(#style,'text-align:left;')][parent::tr[not(#style="display: none")]][./a[#href]]/following-sibling::td[1]
Hey try using this expression for xpath:
(//div[#class='maincounter-number']/span)[1]/text()
for css:
response.css('#maincounter-wrap:nth-child(7) span::text')

How to construct an xpath returning all of a set of elements

Considering theses xpath expressions :
//*[#id="searchResults"]/div[1]/div[1]/h2/span
//*[#id="searchResults"]/div[3]/div[1]/h2/span
//*[#id="searchResults"]/div[5]/div[1]/h2/span
For your info the div inside search result's class is article searchResult and the one inside article searchResult is header.
I am not sure how to construct an xpath matching all three of the above elements. Is there a tool or a how to guide for that?
Thanks
Use position function
//*[#id="searchResults"]/div[position()=1 or position()=3 or position()=5]/div[1]/h2/span
If, by 'all', you mean all div in even position index, then you can use mod operator to check :
//*[#id="searchResults"]/div[position() mod 2 = 1]/div[1]/h2/span
but if 'all' literally means all, then you don't need index to return all matched elements :
//*[#id="searchResults"]/div/div[1]/h2/span

Selenium WebDriver findElement(By.xpath()) not working for me

I've been through the xpath tutorials and checked many other posts, hence I'm not sure what I'm missing. I'm simply trying to find the following element by xpath:
<input class="t-TextBox" type="email" test-id="test-username"/>
I've tried many things, such as:
element = findElement(By.xpath("//[#test-id='test-username']"));
The error is Expression is not a legal expression.
I'm using Firefox on MacBook
Any suggestion would be greatly appreciated.
element = findElement(By.xpath("//*[#test-id='test-username']"));
element = findElement(By.xpath("//input[#test-id='test-username']"));
(*) - means any tag name.
You should add the tag name in the xpath, like:
element = findElement(By.xpath("//input[#test-id='test-username']");
your syntax is completely wrong....you need to give findelement to the driver
i.e your code will be :
WebDriver driver = new FirefoxDriver();
WebeElement element ;
element = driver.findElement(By.xpath("//[#test-id='test-username']");
// your xpath is: "//[#test-id='test-username']"
i suggest try this :"//*[#test-id='test-username']"
You missed the closing parenthesis at the end:
element = findElement(By.xpath("//[#test-id='test-username']"));
Just need to add * at the beginning of xpath and closing bracket at last.
element = findElement(By.xpath("//*[#test-id='test-username']"));
You can use contains too:
element = findElement(By.xpath("//input[contains (#test-id,"test-username")]");
You haven't specified what kind of html element you are trying to do an absolute xpath search on. In your case, it's the input element.
Try this:
element = findElement(By.xpath("//input[#class='t-TextBox' and #type='email' and #test-
id='test-username']");
Correct Xpath syntax is like:
//tagname[#value='name']
So you should write something like this:
findElement(By.xpath("//input[#test-id='test-username']"));

Resources