如何在selenium 4中使用locate_with

发布于 2025-01-17 18:56:45 字数 546 浏览 0 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡看悲欢离合 2025-01-24 18:56:45

由于您能够找到第一个元素,因此我在代码中没有看到任何问题:

decision_div = browser.find_element(By.CLASS_NAME, "decisions")
conclusion_div = browser.find_element(locate_with(By.TAG_NAME,  "div").below(decision_div))

您只需确保 class 属性值为 < 的元素即可code>decisions 位于所需的

元素之上,如下所示:

<sometagname class="decisions" ...></div>
<div class="some class" ...></div>

注意:您必须添加以下导入:

from selenium.webdriver.support.relative_locator import locate_with

参考文献

您可以找到一些相关的详细讨论:

As you are able to find the first element, I don't see any issues in your code as such:

decision_div = browser.find_element(By.CLASS_NAME, "decisions")
conclusion_div = browser.find_element(locate_with(By.TAG_NAME,  "div").below(decision_div))

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:

<sometagname class="decisions" ...></div>
<div class="some class" ...></div>

Note : You have to add the following imports :

from selenium.webdriver.support.relative_locator import locate_with

References

You can find a couple of relevant detailed discussions in:

就是爱搞怪 2025-01-24 18:56:45

硒4 相对定位器和相邻的位置,而不是您在这里尝试做的。
在这种情况下
因此,您只需在此代码行中找到code> condusion_div元素:

conclusion_div = browser.find_element(By.XPATH, "//div[@class='decisions']//div")

但是,由于上面的定位器提供了13个匹配项,我不知道您想找到

conclusion_div = browser.find_element(By.XPATH, "//div[@class='decisions']//div")

匹配

conclusion_div = browser.find_element(By.XPATH, "//*[@class='decisions']//div[contains(@class,'carousel')]")

哪个

conclusion_div = browser.find_element(By.XPATH, "//*[@class='decisions']//div[@class='decision-image']")

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 by decisions class name.
So, you can simply locate the conclusion_div element with this code line:

conclusion_div = browser.find_element(By.XPATH, "//div[@class='decisions']//div")

But since the locator above gives 13 matches and I don't know which of them you want to locate it could be:

conclusion_div = browser.find_element(By.XPATH, "//div[@class='decisions']//div")

Or maybe

conclusion_div = browser.find_element(By.XPATH, "//*[@class='decisions']//div[contains(@class,'carousel')]")

Or maybe

conclusion_div = browser.find_element(By.XPATH, "//*[@class='decisions']//div[@class='decision-image']")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文