简体   繁体   中英

getText() from dropdown which contains linktext is returning empty string

Website https://formy-project.herokuapp.com/
Problem faced during getText() from components dropdown.
code:

@Test(priority=1)
    public void elements () throws InterruptedException{

        List<WebElement> getlinks=driver.findElements(By.tagName("a"));
        int nooflinks=getlinks.size();
        System.out.println("numbers linktext in page:- "+nooflinks);

        for(int i=0;i<=nooflinks-1;i++){

            WebElement alllinks=getlinks.get(i);
            String all_Ltext=alllinks.getText();

            JavascriptExecutor jse=(JavascriptExecutor)driver;
            jse.executeScript("arguments[0].getText();", all_Ltext);
            this block is not working


            System.out.println("Link text:- "+all_Ltext +" \nNumber of charecters in text:-"+allchar +"\nand color are:- "+tcolor);
            d
}

To get text in Selenium, element should be visible. You have to wait until elements will be visible before getting text. WebDriverWait used for specific conditions waits:

import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

WebDriverWait wait = new WebDriverWait(driver, 10);

List<WebElement> links = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("a.dropdown-item")));
// links.forEach(element -> System.out.println(element.getText()));
links.forEach(element -> {
    // Do something ...
    System.out.println("Link text: " + element.getText());
});

There's no JavaScript getText() method, that's why jse.executeScript("arguments[0].getText();", all_Ltext); not working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM