我有一个如下所示的 div 元素。
我使用以下 xpath 标识该元素。
//*[contains(@class,'ce-component-title')]
Selenium 识别该元素并加载 WebElement 对象。但是当我去获取它的文本时,我只得到一个“。”如下所示,而不是获取“购买付款流程”。我在这里做错了什么?我检查了 chrome 控制台,没有其他元素与此 xpath 匹配。
任何帮助将不胜感激。
最佳答案
根据您共享的 HTML,该元素是 Angular元素,因此您必须引发 WebDriverWait 才能使所需的元素可见,并且您可以使用以下解决方案之一:
css选择器
:String myString = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div[ng-if*='getTitle']>div.ce-component-title"))).getAttribute("innerHTML");
xpath
:String myString = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(@ng-if,'getTitle')]/div[@class='ce-component-title flex text-overflow ng-binding']"))).getAttribute("innerHTML");
关于java - 如何使用 Selenium 从 div 元素获取文本仅返回 ".",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53386312/