Eclipse-Selenium-从table中提取值
Eclipse-Selenium-Extract the value from the table
我对测试自动化比较陌生,尤其是对 Selenium。我正在使用 Selenium Web 驱动程序 Eclipse。
我无法从 column/line:
中提取值
我使用以下代码:
定义网络元素:
@FindBy(xpath = "//*[@id=\"pendingreferrals\"]/tbody/tr")
@CacheLookup
private List<WebElement> SignedReferralsTable;
方法:
public void PickReferralFromSignedReferralsTable() throws InterruptedException
{ExplicitWait.until(ExpectedConditions.visibilityOfAllElements(SignedReferralsTable));
Thread.sleep(1000);
Integer rowcount = SignedReferralsTable.size();
Integer rownumber = ThreadLocalRandom.current().nextInt(1, rowcount + 1);
Thread.sleep(5000);
**String WebRefID = GlobalVariables._browser.currentDriver.findElement
(By.cssSelector("#pendingreferrals > tbody > tr:nth-child(" + rownumber + ") > td:nth-
child(1) > a")).getText().toString();**
actions.moveToElement(GlobalVariables._browser.currentDriver.findElement(By.cssSelector("#pendingreferrals > tbody > tr:nth-child(" + rownumber + ") > td:nth-child(9) > a"))).click().perform();
Thread.sleep(5000);
}
在我的代码行中出现错误:
String WebRefID = GlobalVariables._browser.currentDriver.findElement
(By.cssSelector("#pendingreferrals > tbody > tr:nth-child(" + rownumber + ") > td:nth-
child(1) > a")).getText().toString();
产生一个错误:
java.lang.NullPointerException: 无法调用“String.length()”
因为“projAbb”为空
代码截图:
为了清楚起见:我的代码识别 table 中的记录(行)数(SignedReferralsTable 网络元素中的行数),然后它根据总行数决定一个随机行(见变量 rownumber)。
然后它单击 link 视图以导航到此随机记录详细信息网页。
然而,在它被导航之前,我想存储这个特定记录的 ID 号,这个号码是在单独的字符串变量 WebRefID 中随机返回的。这是行不通的。我尝试了不同的方式:
String WebRefID = GlobalVariables._browser.currentDriver.
findElement(By.cssSelector("#pendingreferrals > tbody > tr:nth-child(" + rownumber + ") >
td:nth-child(1) > a")).getText().toString();
String WebRefID = GlobalVariables._browser.currentDriver.
findElement(By.cssSelector("#pendingreferrals > tbody > tr:nth-child(" + rownumber + ") >
td:nth-child(1) > a")).getText();
String WebRefID = GlobalVariables._browser.currentDriver.
findElement(By.cssSelector("#pendingreferrals > tbody > tr:nth-child(" + rownumber + ") >
td:nth-child(1) > a")).toString();
没有任何效果。
我需要存储该值以断言(我计划使用 Assert.assertTrue())我是否导航到了这个特定 order/referral 的页面。
有人可以帮助我吗。
我实际上找到了 cssSelect 无法 identify/recognise 元素的原因。我的错误是在语句的末尾放置了“> a”:
String WebRefID = GlobalVariables._browser.currentDriver.
findElement(By.cssSelector("#pendingreferrals > tbody > tr:nth-child(" +
rownumber + ") > td:nth-child(1) > a")).getText().toString();
删除“> a”后一切开始工作。
对于给您带来的不便,我们深表歉意。
我对测试自动化比较陌生,尤其是对 Selenium。我正在使用 Selenium Web 驱动程序 Eclipse。 我无法从 column/line:
中提取值我使用以下代码:
定义网络元素:
@FindBy(xpath = "//*[@id=\"pendingreferrals\"]/tbody/tr")
@CacheLookup
private List<WebElement> SignedReferralsTable;
方法:
public void PickReferralFromSignedReferralsTable() throws InterruptedException
{ExplicitWait.until(ExpectedConditions.visibilityOfAllElements(SignedReferralsTable));
Thread.sleep(1000);
Integer rowcount = SignedReferralsTable.size();
Integer rownumber = ThreadLocalRandom.current().nextInt(1, rowcount + 1);
Thread.sleep(5000);
**String WebRefID = GlobalVariables._browser.currentDriver.findElement
(By.cssSelector("#pendingreferrals > tbody > tr:nth-child(" + rownumber + ") > td:nth-
child(1) > a")).getText().toString();**
actions.moveToElement(GlobalVariables._browser.currentDriver.findElement(By.cssSelector("#pendingreferrals > tbody > tr:nth-child(" + rownumber + ") > td:nth-child(9) > a"))).click().perform();
Thread.sleep(5000);
}
在我的代码行中出现错误:
String WebRefID = GlobalVariables._browser.currentDriver.findElement
(By.cssSelector("#pendingreferrals > tbody > tr:nth-child(" + rownumber + ") > td:nth-
child(1) > a")).getText().toString();
产生一个错误: java.lang.NullPointerException: 无法调用“String.length()” 因为“projAbb”为空
代码截图:
为了清楚起见:我的代码识别 table 中的记录(行)数(SignedReferralsTable 网络元素中的行数),然后它根据总行数决定一个随机行(见变量 rownumber)。 然后它单击 link 视图以导航到此随机记录详细信息网页。 然而,在它被导航之前,我想存储这个特定记录的 ID 号,这个号码是在单独的字符串变量 WebRefID 中随机返回的。这是行不通的。我尝试了不同的方式:
String WebRefID = GlobalVariables._browser.currentDriver.
findElement(By.cssSelector("#pendingreferrals > tbody > tr:nth-child(" + rownumber + ") >
td:nth-child(1) > a")).getText().toString();
String WebRefID = GlobalVariables._browser.currentDriver.
findElement(By.cssSelector("#pendingreferrals > tbody > tr:nth-child(" + rownumber + ") >
td:nth-child(1) > a")).getText();
String WebRefID = GlobalVariables._browser.currentDriver.
findElement(By.cssSelector("#pendingreferrals > tbody > tr:nth-child(" + rownumber + ") >
td:nth-child(1) > a")).toString();
没有任何效果。 我需要存储该值以断言(我计划使用 Assert.assertTrue())我是否导航到了这个特定 order/referral 的页面。 有人可以帮助我吗。
我实际上找到了 cssSelect 无法 identify/recognise 元素的原因。我的错误是在语句的末尾放置了“> a”:
String WebRefID = GlobalVariables._browser.currentDriver.
findElement(By.cssSelector("#pendingreferrals > tbody > tr:nth-child(" +
rownumber + ") > td:nth-child(1) > a")).getText().toString();
删除“> a”后一切开始工作。
对于给您带来的不便,我们深表歉意。