如何在selenium 4中使用locate_with
我正在尝试在 selenium 4 中使用 相对定位器,但我'我运气不太好。
我在此处找到了一个示例,但即使我能够找到第一个元素,第二行不起作用,它似乎卡在那里(之后我无法自动关闭浏览器)。
decision_div = browser.find_element(By.CLASS_NAME, "decisions")
conclusion_div = browser.find_element(locate_with(By.TAG_NAME, "div").below(decision_div))
如何让它找到具有指定类名的 div 正下方的下一个 div?
I'm trying to use relative locators in selenium 4, but I'm not having much luck with it.
I found an example here, but even though I'm able to find the first element, the second line doesn't work and it seems to be stuck there (I cannot close the browser automatically afterwards).
decision_div = browser.find_element(By.CLASS_NAME, "decisions")
conclusion_div = browser.find_element(locate_with(By.TAG_NAME, "div").below(decision_div))
How can I get it to find the next div right below the one with the specified class name?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您能够找到第一个元素,因此我在代码中没有看到任何问题:
您只需确保 class 属性值为 < 的元素即可code>decisions 位于所需的
元素之上,如下所示:
注意:您必须添加以下导入:
参考文献
您可以找到一些相关的详细讨论:
As you are able to find the first element, I don't see any issues in your code as such:
You just need to ensure that the the element with the value of class attribute as
decisions
is above the desired<div>
element as follows:Note : You have to add the following imports :
References
You can find a couple of relevant detailed discussions in:
硒4 相对定位器和相邻的位置,而不是您在这里尝试做的。
在这种情况下
因此,您只需在此代码行中找到
code> condusion_div
元素:但是,由于上面的定位器提供了13个匹配项,我不知道您想找到
匹配
哪个
Selenium 4 relative locators are dealing with pair of web elements with the similar size and adjacent positions, not what you are trying to do here.
In this case the
div
element you are trying to locate is similarly nested below the parent element located bydecisions
class name.So, you can simply locate the
conclusion_div
element with this code line:But since the locator above gives 13 matches and I don't know which of them you want to locate it could be:
Or maybe
Or maybe