Selenium WebDriver:无法定位动态变化的React Inputbase组件元素
我试图找到具有ID的元素。 开发人员工具显示了以下内容:
<input aria-invalid="false" autocomplete="off" placeholder="Partner name" type="text" class="MuiInputBase-input MuiInput-input MuiAutocomplete-input MuiAutocomplete-inputFocused MuiInputBase-inputAdornedEnd" aria-autocomplete="list" autocapitalize="none" spellcheck="false" value="" id="mui-85700">
ID总是每次重新加载页面时总是更改。
我想我找到了一个解决元素的解决方案:
findElement(By.xpath("//*[contains(@id,'mui')]")).click();
但是现在我收到以下错误:
(org.openqa.selenium.ElementClickInterceptedException: Element <p id="mui-51640" class="MuiTypography-root MuiTablePagination-caption MuiTypography-body2 MuiTypography-colorInherit"> is not clickable at point (1124,747) because another element <div class="MuiDialog-container MuiDialog-scrollPaper"> obscures it
如何解决?
旁注: 在开发人员工具中,我看到了包含“ MUI”字符串的4个Web元素。 在页面上,我只看到2个Web元素:一个带有&lt;输入ID =“ MUI85700” ...&gt;
,另一个是&lt; label ID =“ MUI85700” ... &gt;
和以前说的那样,我需要输入ID字段。
I tried to locate an element with id.
The Developer tools shows this:
<input aria-invalid="false" autocomplete="off" placeholder="Partner name" type="text" class="MuiInputBase-input MuiInput-input MuiAutocomplete-input MuiAutocomplete-inputFocused MuiInputBase-inputAdornedEnd" aria-autocomplete="list" autocapitalize="none" spellcheck="false" value="" id="mui-85700">
The id always changing every time I reload the page.
I think I find a solution to locate the element:
findElement(By.xpath("//*[contains(@id,'mui')]")).click();
But now I get the following error:
(org.openqa.selenium.ElementClickInterceptedException: Element <p id="mui-51640" class="MuiTypography-root MuiTablePagination-caption MuiTypography-body2 MuiTypography-colorInherit"> is not clickable at point (1124,747) because another element <div class="MuiDialog-container MuiDialog-scrollPaper"> obscures it
How can I solve it?
Side note:
In developer tool I see 4 web elements which contains "mui" String.
On the page I only see 2 web elements: one with <input id="mui85700"...>
and another one is <label id="mui85700"...>
As before I said I need the input id field.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(3)
划一舟意中人2025-01-28 03:06:14
你可以这样使用,
List<WebElement> products = driver.findElements(By.xpath("xpath"));
for (int i=0; i<products.size(); i++){
String name = products.get(i).getText();
System.out.println(name);
if(name.containsAll('text')) {
driver.findElements(By.xpath("xpath")).get(i).click();
}
}
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
元素是一个 React InputBase 组件 和动态元素以及
id
属性的尾随动态值将在您每次重新访问应用程序时发生更改。因此,您必须构建动态定位器策略。解决方案
要单击
元素,您需要引发 WebDriverWait< /a> 为 elementToBeClickable() 并且您可以使用以下任一定位器策略:
cssSelector:
xpath:
The
<input>
element is a React InputBase component and a dynamic element and the trailing dynamic value of theid
attribute will will get changed everytime you access the application afresh. So you have to construct a dynamic locator strategy.Solution
To click on the
<input>
element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:cssSelector:
xpath: