Selenium How To Get Text Only From B Tag Inside Class

Find the data you need here

We provide programming data of 20 most popular languages, hope to help you!

Previous PostNext Post

How to get text from inside tag use selenium webdriver java?

Sorted by: 1. You can get the text using the code : String text = driver.findElement (By.cssSelector ("p.font11.metadata_value")).getText () The variable text will contain 639.78 MB value . You can extract 639.78 from the text in several ways like : text.split ("\\s+") [0] // this will return 639.78. OR.

<li id="li1">
  <div>
    <p class="font11 metadata_value">0 Views | 14 Downloads</p>
    <p class="font11 metadata_value">639.78 MB</p>
</div>                      
</li>
String text = driver.findElement(By.cssSelector("p.font11.metadata_value")).getText()
text.split("\\s+")[0] // this will return 639.78
driver.findElement(By.xpath("//li[@id='li1']/div/p[1]")).getText() // for first <p>

driver.findElement(By.xpath("//li[@id='li1']/div/p[2]")).getText() //for second <p>

driver.findElement(By.xpath("//li[@id='li1']/div/p[3]")).getText() // for third <p>

and so on ..
public void verifrytext(String inputext)
{
    WebDriver driver=new ChromeDriver();
    List <WebElement> text = driver.findElements(By.cssSelector(".font11 metadata_value"));

   for(int i=0; i< text.size(); i++)
   {
     if(text.get(i).getText().equalsIgnoreCase(inputext))
     {
       System.out.println(text.get(i).getText());
     }   
   }
}
public class Mytest1 { 
//To open Firefox browser 
WebDriver driver = new FirefoxDriver(); 

@Before public void beforetest() { 
//To Maximize Browser Window 
driver.manage().window().maximize(); 
//To Open URL In browser 
driver.get("http://only-testing-blog.blogspot.in/2013/11/new-test.html"); 
} 



@After public void aftertest() { 
   driver.quit(); 
   } 

   @Test public void test() { 
   driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

   //READ
   String datentime = driver.findElement(By.className("metadata_value")).getText();
   //WRITE
   driver.findElement(By.className("metadata_value")).sendKeys("SOME_STRING");

   }

 }
String text = driver.findElement(By.cssSelector("#li1 > div > p:nth-child(2)")).getText();
Float value = Float.parseFloat(text.replaceFirst(".*?([\\d.]+).*", "$1"));

How to get text of a tag in selenium – Python?

from selenium import webdriver. Step #2: Create a Chrome object or specify web driver path if it is not present in the default path and assign URL. Python3. driver = webdriver.Chrome ('Enter Chrome Path') driver.get ('Enter Web URL') Step #3: Specify the tag name, which you want to extract the text.

Object Name.find_element_by_tag_name(Tag Name)

Selenium: Get text in an element with child containing a specific class

div.some class > p:nth-child (1) to get the text inside it. Since the text is inside the <p> para tag, you can get the text from the first <p> tag. elem = browser.find_element_by_css_selector ("div.some class > p:nth-child (1)") text = elem.text. This should get you the text inside the element.

<div class="some class">
<p>
   <i class="class1"></i>
   Some Text
</p>
<p>
   <i class="class2"></i>
   Some Text
</p>
.  .  .
.  .  .
.  .  .
</div
elem = browser.find_element_by_xpath("//div[@class='some class']")
text = elem.find_element_by_xpath("//p/i[@class='class1']").text
# Find "i" element with "class1" css class and get first parent "p" element
elem = browser.find_element_by_xpath("//i[@class='class1']/ancestor::p[1]")
# Same as previous with added "div"
elem = browser.find_element_by_xpath("//div[@class='some class']//i[@class='class1']/ancestor::p[1]")

# Find "p" element with child "i" element with "class1" css class
elem = browser.find_element_by_xpath("//p[./i[@class='class1']]")
# Same as previous with added "div"
elem = browser.find_element_by_xpath("//div[@class='some class']//p[./i[@class='class1']]")
elem = browser.find_element_by_xpath("//div[@class='some class']")

# Now let's find the i element you want
i_elem = elem.find_element_by_xpath("//i[@class='class1']")

# Now find the parent of that i_elem, which is p
p_elem = [p for p in i_elem.iterancestors() if p.tag=='p'][0]

txt = p_elem.text
xPath = "//div[@class='some class']"
try:
    element = driver.find_element_by_xpath(xPath)
        b1Text = driver.execute_script("return arguments[0].childNodes[2].textContent", element);
        print(b1Text)
except:
       print()
elem = browser.find_element_by_css_selector("div.some class > p:nth-child(1)")
text = elem.text

How to Find Element by Text in Selenium: Tutorial

The code above remains the same except for the method to locate the element. Run Selenium Tests on Real Device Cloud for Free. Replace the text () method with the following code: // located element with contains () WebElement m = driver.findElement (By.xpath ("//* [contains (text (),'Get started ')]")); The method above will locate the “ Get

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class Match{
public static void main(String[] args) {

System.setProperty("<Path of the ChromeDriver>");

WebDriver driver = new ChromeDriver();
String url = "https:/browserstack.com”;
driver.get(url);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

// Locating element with text()
WebElement e = driver.findElement(By.xpath("//*[text()='Get started free']"));

System.out.println("Element with text(): " + e.getText() );
driver.quit();
}
}
// located element with contains()
WebElement m = driver.findElement (By.xpath ("//*[contains(text(),'Get started ')]"));

Selenium select and extract text all possible ways

You can check also: Selenium locator contains text and. firefox 52 with gecko driver 52. In most cases text extraction with selenium can be done simply by: e.getText() Copy. But in some cases this will return nothing - nested tags, wrong tags etc. If you want to extract text in those cases you can try by this examples- java code:

e.getText()
WebElement el = driver.findElements(By.cssSelector("div.myclass"));
String title1 = el.getText()
String title2 = el.text
String title3 = el.getAttribute("textContent")
String title4 = el.getAttribute("innerHTML");
WebElement el = driver.findElements(By.cssSelector("div.myclass"));
String text = el.getAttribute("textContent")
List<WebElement> el = driver.findElements(By.cssSelector("div.myclass"));
for (WebElement e : el4) {
	def title = e.getText()
}
WebElement el = driver.findElement(By.xpath("//div[@class='myclass']//span[@class='yourclass']"));
String text = el.getText()
WebElement el = driver.findElement(By.cssSelector(".myclass .yourclass"));
String text = el.getAttribute("innerHTML");
WebElement el = driver.findElement(By.cssSelector("div.myclass"));
WebElement ee = el.findElement(By.cssSelector("span.yourclass"))
String text = ee.getAttribute("textContent")

Python Selenium – Find element by text

tag: Here, tag stands for the tag name which contains the particular word. word: Here, word stands for the text which has to be found in a particular string. We don’t need to write a complete sentence which we want to find, but we can write a few words which are unique in the web page. Example: For instance, consider this simple page source:

Previous PostNext Post

Related code examples