英文:
How to get a dynamic text in span element in Java Selenium?
问题
I am searching on a site by selecting a date filter over the filter. I'm trying to get the total number of records shown in the filter result.
There is a phrase "Showing 1 to 50 of 130" under the records shown on the site, and I want to dynamically take this phrase and display it on the console.
I tried many things but none of them were successful.
When I take it with InnerHTML it shows the data but all records before filtering. The value of the field changes as I filter, and I want the changed value to be retrieved when I filter.
I tried to get this data with xpath and CSS selector but it didn't work.
I am sharing the final version of my code below.
System.out.println(driver.findElement(By.xpath("//span[contains(@class, 'pagination-summary')]/xpath[1]")));
That's the error message I got:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//span[contains(@class, 'pagination-summary')]/xpath[1]"}
英文:
I am searching on a site by selecting a date filter over the filter. I'm trying to get the total number of records shown in the filter result.
There is a phrase "Showing 1 to 50 of 130" under the records shown on the site, and I want to dynamically take this phrase and display it on the console.
I tried many things but none of them were successful.
When I take it with InnerHTML it shows the data but all records before filtering. The value of the field changes as I filter, and I want the changed value to be retrieved when I filter.
I tried to get this data with xpath and CSS selector but it didn't work.
I am sharing the final version of my code below.
System.out.println(driver.findElement(By.xpath("//span[contains(@class, 'pagination-summary')]/xpath[1]")));
That's the error message i got:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//span[contains(@class, 'pagination-summary')]/xpath[1]"}
答案1
得分: 1
System.out.println(driver.findElement(By.xpath("//span[contains(@class, 'pagination-summary')]")).getText());
英文:
By locator = By.xpath("//span[contains(@class, 'pagination-summary')]");
WebElement element = driver.findElement(locator);
String text = element.getText();
System.out.println(text);
In one single line
System.out.println(driver.findElement(By.xpath("//span[contains(@class, 'pagination-summary')]")).getText());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论