I'm using selenium webdriver to get some text on my webpage using xpath.
This is the code
<a class="ng-binding" data-toggle="tab" href="#tabCreatedByMe">
Created By Me
<span class="badge ng-binding">3</span>
</a>
I need to get the number '3'. this number is changing everytime
I made this code but it does not return anything
public String getAmountSubtab1() throws InterruptedException{
WebElement s = driver.findElement(By.xpath("//*[#class='badge ng-binding']"));
return s.getText(); }
Suggestions?
Are you sure you have only one span with the class badge ng-binding It might be that you might have another span before this with the same class name. Advised not to use class name when identifying an element. Use this xpath. Should work.
//a[contains(text(), 'Created By Me')]/span
Related
I am working on automation and getting dynamic id each and every-time .Please find the sample code..
<li class="list-group-items pingMessage clearfix" id="59580" data-reactid=".c.$0.0.$59580"></li>
<li class="list-group-items pingMessage clearfix" id="59581" data-reactid=".c.$0.0.$59580"><li>
<li class="list-group-items myMessage pingMessage clearfix bunch" id="59588" data-reactid=".e.$0.0.$59578"></li>
Every <li> item has a different id. I want to get an item's id value using gettext then store it in a variable and call in xpath.
I tried following code :
//Getting id of particular text
WebElement Id=driver.findElement("By.xpath(//*[#class='list-group-items myMessage pingMessage clearfix bunch']").getText();
//My problem starts here i want to pass the stored id as id value how can i do it..
driver.findElement("By.id("+Id+")).click;
getText() is used to get the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing whitespace while you need here to get element id attribute, So you should try using getAttribute() as below :-
//Storing the value of ID
WebElement Id = driver.findElement(By.cssSelector(".list-group-items.myMessage.pingMessage.clearfix.bunch")).getAttribute("id");
Please try with below xpath
//tagName[contains(#id='595')]
I am working on a Spring MVC application that uses Thymeleaf as template engine and I am trying to capitalize some string showed into my page. On my page I have something like this:
<li class="com__nav-item" th:each="menuItem : ${#authentication.principal.listaFunzioniUtente}">
<a href="" class="com__nav-link centered">
<span class="blue-line animate scaleIn delay-3" style="font-size: 1.4em; text-align: center;" th:text="${#strings.capitalize(menuItem.desFnz)}"></span>
<span class="white-circle animate scaleIn delay-5"></span>
</a>
</li>
As you can see in the previous code, in the first <span> tag, I show a string inside the desFnz property of the menuItem object.
It works fine, my problem is that I want capitalize all the characters, so I tried to do:
th:text="${#strings.capitalize(menuItem.desFnz)}"
using the #strings.capitalize() but it can't work, in fact in my page I still obtain the text but not capitalized. Why? What am I missing? How can I fix this issue?
#strings.capitalize(menuItem.desFnz) will only capitalize the 1st character, where as #strings.toUpperCase(menuItem.desFnz) will convert the entire string to uppercase. Here is the documentation for the Strings class.
you can do it by
$string.toLowerCase() or $string.toUpperCase()
Just adding to Pradeep Pati's point.
In case you are using it in the spring boot project where some of your values are coming from messages.properties
like In messages.properties file, You have something like:
email.dailyAlert.greeting.newTemplate = Dear {0},
Then to substitute the value in place of {0} (in the Title case), you need to write like the below line.
<p th:text="#{email.dailyAlert.greeting.newTemplate(${#strings.capitalize(orgSlug)})}"></p>
The final output will be:
Dear Organisation,
A webpage contains
<div class="divclass">
<ul>
<li>
"hello world 1"
<img src="abc1.jpg">
</li>
<li>
"hello world 2"
<img src="abc2.jpg">
</li>
</ul>
</div>
I am able to get data under div using
element = driver.find_element(class: "divclass")
element.text.split("\n")
But I want all links respective to the achieved data
I tried using
driver.find_elements(:css, "div.divclass a").map(&:text)
but failed.
How can I get related links to the data?
If you want to get the href attribute try the below code(I am not familiar with ruby so I am posting the code in Java).
List<WebElement> elements = driver.findElements(By.xpath("//*[#class='divclass']//a"));
for(WebElement webElement:elements){
System.out.println(webElement.getAttribute("href"));
}
The xpath points to all the a tags under the div tag with class name =divclass.
If you want to get the text of all the links, you can use the blow code:
List<WebElement> elements = driver.findElements(By.xpath("//*[#class='divclass']//a"));
for(WebElement webElement:elements){
System.out.println(webElement.getText());
}
Hope it helps.
In ruby
element = driver.find_elements(:xpath, "//*[#class='divclass']//a")
list = element.collect{|e| hash ={e.text => e.attribute("href")}}
will return corresponding links with data in array of hashes
<div class="a-row a-spacing-micro" style="">
<i class="a-icon a-icon-star-medium a-star-medium-4"></i>
<a data-analytics="{"name":"Review.FullReview"}" class="a-size-base a-link-normal a-color-base review-title a-text-bold" href="/gp/cdp/member-reviews/A19123D9G66E0O/ref=pdp_new_read_full_review_link?ie=UTF8&page=1&sort_by=MostRecentReview#R1Z0A6K9CROFFV"> <span>Good Cheap Knee Pads</span>
</a>
</div>
I have this HTML that I am scraping with XPath. What XPath would I use to just return the class "a-star-medium-4"?
Thanks!
Jeff
If it's only for this specific HTML, you can extract the class name starting with a-star with this XPath:
substring(string(//i/#class),string-length(substring-before(string(//i/#class),'a-star')) +1)
When applied to your example HTML this returns a-star-medium-4.
As explanation: string(//i/#class) returns the class attribute value a-icon a-icon-star-medium a-star-medium-4. To get only the class name starting with a-star, substring() is used to remove the part of the string before a-star by cutting the string after the string-length() of the remaining string when it's cutted before a-star using substring-before().
how to select custom dropdown list element from selenium.
I want to select a country from the dropdown list, using selenium python webdrive.
<select id="id_country" class="hidden-field" name="country" data-id="1394114974464-fOg4n">
<div class="custom dropdown" data-id="1394114974464-fOg4n">
<a class="current" href="#">Belize</a>
<a class="selector" href="#"></a>
<ul>
<li class="">Select Your Country</li>
<li class="">Afghanistan</li>
<li>Albania</li>
<li class="">Algeria</li>
<li class="">American Samoa</li>
<li class="">Andorra</li>
<li>Angola</li>
<li class="">Anguilla</li>
<li class="">Antigua and Barbuda</li>
<li class="">Argentina</li>
<li class="">Armenia</li>
The currently recommended method of selecting an item from a dropdown menu is to use the Select class. Here's a quick example:
from selenium.webdriver.support.ui import Select
from selenium import webdriver
browser = webdriver.Firefox()
browser.get("file:///C:\testing\\test.html")
element = browser.find_element_by_id("id_country")
select = Select(element)
select.select_by_visible_text("Armenia")
However, the HTML you posted doesn't seem to work; I just get an empty dropdown box. You'll need to fix that in order to be able to use the above code. For example:
<html><body>
<select id="id_country" name="country">
<option>Select Your Country</option>
<option>Afghanistan</option>
<option>Armenia</option>
</select>
</body></html>
This works fine for me, producing a dropdown box with three options - Select Your Country, Afghanistan and Armenia. Pointing the above Python script at this file correctly selects Armenia.
Edit: Here's a quick-and-dirty Python script that successfully selects Armenia from the list of countries:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get("http://ds.arm.com/rfq/")
country = "Armenia"
dropdown_path = "id('content')/form/fieldset/div[5]/div[2]/div/a[2]"
country_path = "id('content')/form/fieldset/div[5]/div[2]/div/ul/li[contains(text(), '%s')]" % country
browser.find_element_by_xpath(dropdown_path).click()
browser.find_element_by_xpath(country_path).click()
It makes use of XPaths to locate the dropdown arrow and then the <li> tag containing "Armenia". I don't claim that it's especially intuitive, or neat to look at, but it works. They've really over-complicated things with that website, so I'm not sure if the simpler Select method can be made to work here. In general, if you can locate an element using an id (find_element_by_id), class (find_element_by_class) or name (find_element_by_name), you should do so. None of those work here, unfortunately :)
I can recommend the XPath Checker extension for Firefox to help you find similar XPaths for the other form elements. Good luck, I really don't envy you having to work with that site!!