1

I have the following HTML code

<div class="mbsc-sc-whl-c" style="height:40px;margin-top:-21px;">
 <div class="mbsc-sc-whl-sc" style="top: 0px; transform: translate3d(0px, -1520px, 0px); transition: -webkit-transform 300ms cubic-bezier(0.19, 1, 0.22, 1) 0s; margin-top: -80px;
 <div role="option" tabindex="-1" aria-selected="true" class="mbsc-sc-itm mbsc-btn-e mbsc-sc-itm-sel" data-index="8" data-val="8" style="height:40px;line-height:40px;">08
 <div role="option" tabindex="0" aria-selected="true" class="mbsc-sc-itm mbsc-btn-e mbsc-sc-itm-sel" data-index="9" data-val="9" style="height:40px;line-height:40px;">09

I would like to extract the value of data-val="9" and assign it to a String variable.

I tried

driver.findElement(By.xpath("//div[@role='option']//*[@tabindex = '0']")).getText();

and I am getting "Unable to locate element"

Hope to have advice. Thanks

IAmMilinPatel
7,7667 gold badges44 silver badges68 bronze badges
asked Jun 1, 2022 at 5:22

2 Answers 2

1

QA automation engineers face this issue while automating user interface testing scenarios.

Below is the code that can be used to fetch the value of data-val="9":

String text = driver.findElement(By.xpath("//div[@role='option' and @tabindex = '0']")).getText();

or

String text = driver.findElement(By.xpath("//div[@class='mbsc-sc-whl-sc']/div[@data-val='9']")).getText();

answered Jun 23, 2022 at 14:38
1
  • Thanks @Anand, it did help. However, do you mind sharing with me what is the difference between your code and mine ? Thanks Commented Jul 1, 2022 at 6:47
0

I checked your HTML code and the java code. I have found a few issues in the codes.

<div class="mbsc-sc-whl-sc" style="top: 0px; transform: translate3d(0px, -1520px, 0px); transition: -webkit-transform 300ms cubic-bezier(0.19, 1, 0.22, 1) 0s; margin-top: -80px;
  1. In this line above you have not closed the <div> tag

  2. transform: translate3d(0px, -1520px, 0px); this style and margin-top: -80px; this attribute doesn't allow the text to be displayed within the visible area of the page. Hence Selenium isn't able to get the content of the <div> tag.

Change it to,

<div class="mbsc-sc-whl-sc" style="top: 0px; transform: translate3d(0px, -1520px, 0px); transition: -webkit-transform 300ms cubic-bezier(0.19, 1, 0.22, 1) 0s; margin-top: -80px;>

Now about your java code:

driver.findElement(By.xpath("//div[@role='option']//*[@tabindex = '0']")).getText();

In this line above the Xpath is wrong and hence you are getting the error Unable to locate element".

Here is the correct xpath you can use: //div[@role='option' and @tabindex='0']

Here is my HTML with which I tested:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
 <div class="mbsc-sc-whl-c" style="height:40px;margin-top:-21px;">
 <div class="mbsc-sc-whl-sc" style="top: 0px; transition: -webkit-transform 300ms cubic-bezier(0.19, 1, 0.22, 1) 0s;">
 <div role=" option" tabindex="-1" aria-selected="true" class="mbsc-sc-itm mbsc-btn-e mbsc-sc-itm-sel"
 data-index="8" data-val="8" style="height:40px;line-height:40px;">08</div>
 <div role="option" tabindex="0" aria-selected="true" class="mbsc-sc-itm mbsc-btn-e mbsc-sc-itm-sel"
 data-index="9" data-val="9" style="height:40px;line-height:40px;">09
 </div>
 </div>
 </div>
</body>
</html>

Here is my Java + Selenium code which worked for me:

try {
 WebDriver driver;
 System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "//geckodriver");
 System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, "FFLogs.txt");
 driver = new FirefoxDriver();
 driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 driver.get("http://localhost/1");
 driver.manage().window().maximize();
 Thread.sleep(2000);
 String text = driver.findElement(By.xpath("//div[@role='option' and @tabindex='0']")).getText();
 System.out.println("Your string: " + text);
 String text1 = driver.findElement(By.xpath("//div[@role='option' and @tabindex='0']")).getAttribute("data-val");
 System.out.println("Your attribute: " + text1);
 
 Thread.sleep(5000);
 driver.close();
} catch (Exception e) {
 e.printStackTrace();
}
answered Jun 1, 2022 at 7:44
1
  • Thanks @IAmMilimPatel. It did guide me to the correct direction Commented Jun 17, 2022 at 3:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.