简体   繁体   English

如何从 Xpath 获取 id 作为文本

[英]how to get the id from the Xpath as a text

I had taken the xPath as below but I am not able to get the value 145666 when I try to print.我采用了如下所示的xPath ,但是当我尝试打印时无法获得值145666

WebDriverWait wait = new WebDriverWait(driver, 20);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(@class,'mat-content')]//div[@id='demandTrackingID']")));

Now iam getting the below Expception:现在我得到以下 Expception:

org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: //span[contains(@class,'mat-content')]//div[@id='TrackID'] org.openqa.selenium.TimeoutException:预期条件失败:等待 By.xpath 定位的元素的可见性://span[contains(@class,'mat-content')]//div[@id='TrackID']

Note: This Html Element is a Non Visible Element I need to inspect the panel and get this element as there is no field for this Element注意:这个 Html 元素是一个不可见元素,我需要检查面板并获取这个元素,因为这个元素没有字段

HTML: HTML:

        <div _ngcontent-wbh-c179="" id="TrackID" class="disp-none">14566 </div>

Please try this:请试试这个:

WebElement trackid= driver.findElement(By.xpath("//span[contains(@class,'mat-content')]//div[@id='TrackID']"));
System.out.println("getid:"+ trackid.getText());

You will possible need to add an explicit wait before that to make element fully loaded before retrieving it's content您可能需要在此之前添加一个显式等待,以便在检索内容之前使元素完全加载

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(@class,'mat-content')]//div[@id='TrackID']")));
WebElement trackid= driver.findElement(By.xpath("//span[contains(@class,'mat-content')]//div[@id='TrackID']"));
System.out.println("getid:"+ trackid.getText());

id is ID not id idID不是id

so use this:所以用这个:

WebElement trackid= driver.findElement(By.id("TrackID"));
System.out.println("getid:"+ trackid.getText());

The element that contains the text you want has an ID, TrackID , not to be confused with the "ID" text on the page.包含所需文本的元素有一个 ID TrackID ,不要与页面上的“ID”文本混淆。 You can use that to locate the element more easily than the XPath you are currently using.与您当前使用的 XPath 相比,您可以使用它更轻松地定位元素。 Once you have the element, you can use WebDriverWait to wait for the element to contain text.获得元素后,您可以使用WebDriverWait等待元素包含文本。 Code is below.代码如下。

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until((ExpectedCondition<Boolean>) d -> d.findElement(By.id("TrackID")).getText().length() != 0);

Given the class class="disp-none" on the element, I'm guessing that the element might not be visible.鉴于元素上的 class class="disp-none" ,我猜测该元素可能不可见。 If this is the case, you'll have to use JavaScript to get the invisible text.如果是这种情况,您将不得不使用 JavaScript 来获取不可见的文本。 Selenium was designed to only interact with visible elements and will throw an exception if you try to interact with elements that are not visible. Selenium 旨在仅与可见元素交互,如果您尝试与不可见元素交互,则会抛出异常。

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until((ExpectedCondition<Boolean>) d -> d.findElement(By.id("TrackID")).getText().length() != 0);
WebElement element = driver.findElement(By.id("TrackID"));
String trackID = (JavascriptExecutor)driver.executeScript("return arguments[0].text", element);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM