Home > database >  Eclipse-Selenium-Extract the value from the table
Eclipse-Selenium-Extract the value from the table

Time:05-10

I am relatively new to the Test Automation and in particular to the Selenium. I am using Selenium Web-driver, Eclipse. I can not extract the value from the column/line:

enter image description here

I use the following code:

To define web element:

    @FindBy(xpath = "//*[@id=\"pendingreferrals\"]/tbody/tr")
    @CacheLookup
    private List<WebElement> SignedReferralsTable;

Method:

    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); 
}

In my code line I am getting an error:

    String WebRefID = GlobalVariables._browser.currentDriver.findElement
    (By.cssSelector("#pendingreferrals > tbody > tr:nth-child("   rownumber   ") > td:nth- 
    child(1) > a")).getText().toString(); 

creates an error: java.lang.NullPointerException: Cannot invoke "String.length()" because "projAbb" is null

enter image description here

screenshot of the code:

enter image description here

To make it clear: My code identifies number of the records (rows) in the table (number of rows in SignedReferralsTable web element), then it decides a random row considering the number of the total rows (see variable rownumber). Then it clicks on the link View to navigate to this random record details web page. However before it is navigated I would like to store the ID number of this particular record which number was randomly returned in the separate string variable WebRefID. This does not work. I tried it in different ways:

    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();

Nothing worked. I need to store the value in order to assert (I plan to use Assert.assertTrue())whether I navigated to the page of exactly this particular order/referral. Can somebody kindly help me.

CodePudding user response:

I actually found the reason of cssSelect not being able to identify/recognise the element. My mistake was to put "> a" at the end of the statement:

    String WebRefID = GlobalVariables._browser.currentDriver.
    findElement(By.cssSelector("#pendingreferrals > tbody > tr:nth-child("   
    rownumber   ") > td:nth-child(1) > a")).getText().toString();

After "> a" was removed everything started to work.

Sorry for the inconvenience.

  • Related