英文:
How do I grab text from Selenium (Java)?
问题
I am trying to grab the text $1.00 from the following HTML code (I have the xpath, so don't worry about that). For this purpose, we can say the xpath is //*[@id="price-string"]
$1.00
I have tried using driver.findElement(By.xpath("//[@id="price-string"]")) followed by .gettext(), .getAttribute("textContent"), .getAttribute("innerHTML");
All of them return null, which means they could not find it.
I saw this post here on stackexchange: https://sqa.stackexchange.com/questions/30627/how-to-get-text-under-strong-tag-in-selenium-webdriver-using-java that might help. They say problem is, you cannot directly target/find text nodes with Selenium WebDriver, only regular element nodes. How would you be able to implement a fix in java? Thanks!
英文:
I am trying to grab the text $1.00 from the following HTML code (I have the xpath, so don't worry about that). For this purpose, we can say the xpath is //*[@id="price-string"]
<strong id="price-string">$1.00</strong>
I have tried using driver.findElement(By.xpath("//*[@id="price-string"]")) followed by .gettext(), .getAttribute("textContent"), .getAttribute("innerHTML");
All of them return null, which means they could not find it.
I saw this post here on stackexchange: https://sqa.stackexchange.com/questions/30627/how-to-get-text-under-strong-tag-in-selenium-webdriver-using-java that might help. They say problem is, you cannot directly target/find text nodes with Selenium WebDriver, only regular element nodes. How would you be able to implement a fix in java? Thanks!
答案1
得分: 0
尝试以下代码 -
Thread.sleep(2000);
String word = driver.findElement(By.xpath("//strong[@id='price-string']")).getText();
System.out.println(word);
注意 - 如果这是您要寻找的内容,请将其标记为答案。
英文:
Try below code -
Thread.sleep(2000);
String word = driver.findElement(By.xpath("//strong[@id='price-string']")).getText();
System.out.println(word);
Note - if this is what you are looking for then please mark this as an answer.
答案2
得分: 0
在提取值之前执行等待语句,然后等待页面完全加载。
尝试这样做:
Thread.sleep(5000);
String value = driver.findElement(By.xpath("//strong[@id='price-string']")).getText();
System.out.println(value);
英文:
Do a wait statement before extracting the value and then wait for page fully loaded
Try this:
Thread.sleep(5000);
String value = driver.findElement(By.xpath("//strong[@id='price-string']")).getText();
System.out.println(value);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论