Unable to get the right worth from the desk utilizing css selector in selenium

0
16
Unable to get the right worth from the desk utilizing css selector in selenium


I’m assuming you are attempting to pick the Complete worth from the primary desk. Briefly, the rationale for the failure is that your css selector for desk was not particular sufficient and it was deciding on greater than 1 desk Aspect which causes undesired outcomes.

In your case, I’d recommend you to do the next:

  1. Be certain that the selector you might be formulating is exclusive if you wish to cope with a single factor. The category you used to pick the desk was identical for 4 tables and that is without doubt one of the causes you bought undesired outcomes.

div[class="cb-col cb-col-100 cb-ltst-wgt-hdr"] selects the 4 tables as a substitute of choosing simply the first one.
Verify the variety of outcomes for the selector in chrome.
enter image description here

  1. At any time when attainable, use id locators. They’re quicker and largely distinctive. The rationale I’m telling you that is your desk has a father or mother with an id locator and utilizing that will have helped you with a novel locator for choosing the desk.

This can be a distinctive selector for the primary desk – #innings_1 > div:first-child

  1. It’s higher to retailer the Aspect right into a variable if you’ll use it greater than as soon as.
    You’re utilizing the desk factor twice. Here is an instance.

    WebElement desk = driver.findElement(By.cssSelector("div[class="cb-col cb-col-100 cb-ltst-wgt-hdr"]"));
    String textextras = desk.findElement(By.cssSelector("div:nth-last-child(3) div:nth-child(2)")).getText();
    System.out.println("textextras = "+textextras);
    String textsum = desk.findElement(By.cssSelector("div:nth-last-child(2) div:nth-child(2)")).getText();
    
  2. Practise extra with css locators.

Here is the modified code. I simply modified the selector for the desk!

    WebElement desk = driver.findElement(By.cssSelector("#innings_1 > div:first-child"));
    String textextras = desk.findElement(By.cssSelector("div:nth-last-child(3) div:nth-child(2)")).getText();
    System.out.println("textextras = "+textextras);
    String textsum = desk.findElement(By.cssSelector("div:nth-last-child(2) > div:nth-last-child(2)")).getText();
    System.out.println("textsum = "+textsum);

LEAVE A REPLY

Please enter your comment!
Please enter your name here