返回完整用户名的方法

nfg76nw0  于 2021-08-25  发布在  Java
关注(0)|答案(3)|浏览(315)

我试图创建标签的webelements列表并对其进行迭代,但该方法需要适应调用该方法时传递的不同值。
应该是这样的

public String getName(WebDriver driver, String fieldName)
{
  String fullName= null;
  .
  .
  .
  return fullName;
}
erhoui1w

erhoui1w1#

您必须找到父元素两次,才能从所需的输入字段中获得标记“small”。

package selenium;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class NatureValue extends WebDriverBase {

    public static void main(String[] args) {

        WebDriver driver = startChromeDriver();
        driver.get("https://qa.employeenavigator.com/benefits/Account/Register");
        String[] fieldNames = {"FirstName", "LastName", "CompanyIdentifier", "Last4OfSSN", "BirthDate"};
        for (String fieldName: fieldNames) {
            System.out.println("Field named '" + fieldName + "' has text: " + getSmallTxt(driver, fieldName));
        }
        driver.quit();

    }

    public static String getSmallTxt(WebDriver driver, String fieldName) {
        String smallTxt = null;
        List<WebElement> fieldElements = driver.findElements(By.name(fieldName));
        if (fieldElements.size() > 0) {
            WebElement fieldElement = fieldElements.get(0);
            WebElement parentElement = getParentElement(driver, fieldElement);
            WebElement parentParentElement = getParentElement(driver, parentElement);
            List<WebElement> smallTxtElements = parentParentElement.findElements(By.tagName("small"));
            if (smallTxtElements.size() > 0) {
                WebElement smallTxtElement = smallTxtElements.get(0);
                smallTxt = smallTxtElement.getText();
            }
        }
        if (smallTxt == null) {
            smallTxt = "No text found";
        }
        if (smallTxt.length() < 1) {
            smallTxt = "No text found";
        }
        return smallTxt;
    }

    public static WebElement getParentElement(WebDriver driver, WebElement element) {
        JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;  
        WebElement parentElement = null;
        parentElement = (WebElement) javascriptExecutor.executeScript("return arguments[0].parentNode;", element);
        return parentElement;
    }

}

输出:

Starting ChromeDriver 91.0.4472.101 (af52a90bf87030dd1523486a1cd3ae25c5d76c9b-refs/branch-heads/4472@{#1462}) on port 26426
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Čvc 07, 2021 8:51:23 DOP. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Field named 'FirstName' has text: No text found
Field named 'LastName' has text: No text found
Field named 'CompanyIdentifier' has text: (provided by HR)
Field named 'Last4OfSSN' has text: (Last 4 Digits of SSN / ID)
Field named 'BirthDate' has text: (mm/dd/yyyy)
uurity8g

uurity8g2#

我不理解你的问题,但我假设你想要输入字段中的小写单词。我希望下面的代码将帮助你。

public String getSmallTxt(WebDriver driver, String fieldName) {
        StringBuilder smallTxt = new StringBuilder();
        for (int i = 0; i < fieldName.length(); i++) {
            char ch = fieldName.charAt(i);
            smallTxt.append(ch >= 'a' && ch <= 'z' ? (char) (ch ) : "");
        }
        if(smallTxt.length()==0) {
            return "No text found ";
        }else {
              return smallTxt.toString();
        }
    }
uxh89sit

uxh89sit3#

Can you try the below code and check if it helps

public static List<String> getSmallText(String locator) {
    driver.get("https://qa.employeenavigator.com/benefits/Account/Register");
    List<WebElement> element = driver.findElements(By.tagName(locator));
    List<String> str = new ArrayList<String>();
    if (element.size() == 0) {
        System.out.println("No text found");
    } else {
        for (WebElement e : element) 
            str.add(e.getText());
        return (str);
    }
    return str;
}

相关问题