I’m assuming you are attempting to pick the Whole worth from the primary desk. Briefly, the explanation for the failure is that your css selector for desk was not particular sufficient and it was choosing greater than 1 desk Factor which causes undesired outcomes.
In your case, I’d counsel you to do the next:
- Make sure that the selector you’re formulating is exclusive if you wish to take care of a single ingredient. The category you used to pick the desk was identical for 4 tables and that is likely 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.
- Each time potential, use id locators. They’re quicker and largely distinctive. The explanation I’m telling you that is your desk has a mother or father with an id locator and utilizing that might 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
-
It’s higher to retailer the Factor right into a variable if you’re going to use it greater than as soon as.
You’re utilizing the desk ingredient twice. This 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();
-
Practise extra with css locators.
This 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);