如何使用 Java 中的 Selenium WebDriver 在动态生成的 HTML 代码中通过定位器(ID、名称、XPath 或 CSS)来定位元素?

问题描述 投票:0回答:1
java angular selenium-webdriver ionic-framework
1个回答
0
投票

这取决于您的应用程序的行为。

对于动态元素,我们首先检查的是模式。

哪些属性值保持不变?

//button[@class='action-sheet-button ion-activatable ion-focusable sc-ion-action-sheet-md']

如果不是整个属性值,那么属性的哪一部分保持不变?

//button[contains(@class='action-sheet-button')]

如果您想单击元素按钮内的图像:

//button[contains(@class='action-sheet-button')]//*[@role='img']

如果您想仅使用一个动态 Xpath 单击元素:

'When declaring dynamic element
By dynamicBtnClassRole = By.xpath("//button[contains(@class='%s')]//*[@role='%s']");

'Then when you find for an element
WebElement elmButton = driver.findElement(dynamicBtnClassRole.asBy(strClass, strRole);
elmButton.click();

答案取决于您想要实现的目标

© www.soinside.com 2019 - 2024. All rights reserved.