I am trying to get the title of a table using the below code:
Code snippet:
WebElement SecondItable = driver.findElement(By.xpath("//div[@id='innings_2']//div[@class='cb-col cb-col-100 cb-ltst-wgt-hdr'][1]"));
String TeamInnings2 = SecondItable.findElement(By.xpath("//div[@class='cb-col cb-col-100 cb-scrd-hdr-rw']//child::span[1]")).getText();
String TeamInnings1 = SecondItable.findElement(By.cssSelector("div[class='cb-col cb-col-100 cb-scrd-hdr-rw'] span:nth-child(1)")).getText();
System.out.println(TeamInnings2);
System.out.println(TeamInnings1);
The output value in TeamInnings2 and TeamInnings1 are different (as given below):
When I removed the SecondITable reference and used the entire path (as given below), the xpath is giving the current results. String TeamInnings2 = driver.findElement(By.xpath("//div[@id='innings_2']//div@class='cb-col cb-col-100 cb-ltst-wgt-hdr'//div[@class='cb-col cb-col-100 cb-scrd-hdr-rw']//child::span1")).getText();
I am very new to Selenium. Can anyone explain me why the results are different when the xpath and css selectors are pointing to the same element? Why the xpath is giving correct results with the entire path but not with the table reference?
1 Answer 1
You are using the below xpath:
"//div[@class='cb-col cb-col-100 cb-scrd-hdr-rw']//child::span[1]"
as you are using //div , the xpath search will start for div element from anywhere in HTML and not from the parent SecondItable as you expected.
To search for div under parent SecondItable use below xpath:
"./div[@class='cb-col cb-col-100 cb-scrd-hdr-rw']//child::span[1]"
Here we used ./ instead of //, telling XPath to search from current node which his the SecondItable
so final will look like:
WebElement SecondItable = driver.findElement(By.xpath("//div[@id='innings_2']//div[@class='cb-col cb-col-100 cb-ltst-wgt-hdr'][1]"));
String TeamInnings2 = SecondItable.findElement(By.xpath("./div[@class='cb-col cb-col-100 cb-scrd-hdr-rw']//child::span[1]")).getText();
String TeamInnings1 = SecondItable.findElement(By.cssSelector("div[class='cb-col cb-col-100 cb-scrd-hdr-rw'] span:nth-child(1)")).getText();
System.out.println(TeamInnings2);
System.out.println(TeamInnings1);
Note:
String TeamInnings2 = SecondItable.findElement(By.xpath("./div[@class='cb-col cb-col-100 cb-scrd-hdr-rw']//child::span[1]")).getText();
The above locator will find a div element which is immediate child to parent eg secoditable>div but it will not find secoditable>li>div as div is not a immediate child.
So use :
String TeamInnings2 = SecondItable.findElement(By.xpath(".//div[@class='cb-col cb-col-100 cb-scrd-hdr-rw']//child::span[1]")).getText();
here .// ensures that the xpath searchs for div under parent but not just for immediate child.
-
My friend, PDHide... Brilliant analysis.... Thanks a lot! I really appreciate your time and help in resolving this issue. You are the man to go in the time of crisis..SelfLearner– SelfLearner2020年04月09日 08:03:21 +00:00Commented Apr 9, 2020 at 8:03
-
I need one more help from you. I have used the above logic (./div) for two more fields, but I am facing the following error. My code: String Ext = SecondItable.findElement(By.xpath("./div[text()='Extras']/following-sibling::div[1]")).getText(); String ActualSc = SecondItable.findElement(By.xpath("./div[text()='Total']/following-sibling::div")).getText(); errors: Cannot locate an element using xpath=./div[text()='Extras']/following-sibling::div[1] Cannot locate an element using xpath=./div[text()='Total']/following-sibling::div[1]SelfLearner– SelfLearner2020年04月09日 08:03:26 +00:00Commented Apr 9, 2020 at 8:03
-
./div identifies immediate child under the parent , use .// instead so . will ensure that it searching from parent and // esures that anywhere under parentPDHide– PDHide2020年04月09日 08:18:54 +00:00Commented Apr 9, 2020 at 8:18
-
Updated the answer, could you accept the answer by clicking the tick sign near to my answer . Thanks happy to helpPDHide– PDHide2020年04月09日 08:21:51 +00:00Commented Apr 9, 2020 at 8:21
-
1I am very new to this website so I was not aware that I had to accept the answer. I have accepted the answer now.SelfLearner– SelfLearner2020年04月09日 08:27:39 +00:00Commented Apr 9, 2020 at 8:27
Explore related questions
See similar questions with these tags.
String TeamInnings2 = SecondItable.findElement(By.xpath("./div[@class='cb-col cb-col-100 cb-scrd-hdr-rw']//child::span[1]")).getText();