Software Quality Assurance & Testing Asked on February 27, 2021
I am trying to get the value displayed against the Total using the below code:
driver.get("https://www.cricbuzz.com/live-cricket-scorecard/20137/sl-vs-eng-4th-odi-england-tour-of-sri-lanka-2018");
String textextras = driver.findElement(By.cssSelector("div[class='cb-col cb-col-100 cb-ltst-wgt-hdr']")).findElement(By.cssSelector("div:nth-last-child(3) div:nth-child(2)")).getText();
System.out.println(textextras);
String textsum = driver.findElement(By.cssSelector("div[class='cb-col cb-col-100 cb-ltst-wgt-hdr']")).findElement(By.cssSelector("div:nth-last-child(2) div:nth-child(2)")).getText();
System.out.println(textsum);
Output:
19 Batsman R B 4s 6s SR
Can anyone please help me why it is not working for textsum, but it is working fine for textextras variable ?
Also had unexpected behavior of .getText()
in past, so I would advice to use element.getAttribute("innerText")
as described here. It resolved my cases when result strings were empty.
I use element.findElement(By.xpath(".//*[@class='title']")).getAttribute("innerText");
to find any node with class equals 'title' and get that title.
For your tables, there may be used following xpath ("//div[contains(@class,'cb-scrd-itms')]/div[last()]")
, that locates all lines and takes last-child div.
Answered by NickG on February 27, 2021
I am assuming you are trying to select the Total value from the first table. In short, the reason for the failure is that your css selector for table was not specific enough and it was selecting more than 1 table Element which causes undesired results.
In your case, I would suggest you to do the following:
div[class='cb-col cb-col-100 cb-ltst-wgt-hdr']
selects the 4 tables instead of selecting just the 1st one.
Check the number of results for the selector in chrome.
This is a unique selector for the first table - #innings_1 > div:first-child
It is better to store the Element into a variable if you are going to use it more than once. You are using the table element twice. Here's an example.
WebElement table = driver.findElement(By.cssSelector("div[class='cb-col cb-col-100 cb-ltst-wgt-hdr']"));
String textextras = table.findElement(By.cssSelector("div:nth-last-child(3) div:nth-child(2)")).getText();
System.out.println("textextras = "+textextras);
String textsum = table.findElement(By.cssSelector("div:nth-last-child(2) div:nth-child(2)")).getText();
Practise more with css locators.
Here's the modified code. I just changed the selector for the table!
WebElement table = driver.findElement(By.cssSelector("#innings_1 > div:first-child"));
String textextras = table.findElement(By.cssSelector("div:nth-last-child(3) div:nth-child(2)")).getText();
System.out.println("textextras = "+textextras);
String textsum = table.findElement(By.cssSelector("div:nth-last-child(2) > div:nth-last-child(2)")).getText();
System.out.println("textsum = "+textsum);
Answered by Renju Jose on February 27, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP