首页 > 解决方案 > 使用 selenium 查找站点上其他地方使用的具有多个类的 div。(Python)

问题描述

所以基本上我有这样的东西......

<div class="123">ex1</div>
<div class="456">ex2</div>
<div class="789">ex3</div>
<div class="100">ex4</div>
<div class="123 456 789 100">ex5</div>
    <a aria-label="Title" class="the same problem"></a>

我想选择第 5 个 div,而不选择其他 4 个。

我见过一些人这样做:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

driver.findElement(By.cssSelector("div.123.456.789.100"));

但是当我尝试安装 org 模块时,出现“找不到满足 org 要求的版本”错误。

所以我发现了这个:

driver.find_elements_by_xpath("//*[@class='123' or @class='456' or @class='789' or @class='100']")

但我也得到了

"selenium.common.exceptions.NoSuchElementException: 消息:没有这样的元素:无法找到元素:{"method":"xpath","selector":"//*[@class='123' 或 @class='456 ' 或 @class='789' 或 @class='100']"}"

错误。

我已经完成了我的研究,没有任何帮助,所以我决定在这里问。

另外我想稍后从那个“a”对象中获取 aria-label 文本,它有我之前描述的相同问题。我可能会自己弄清楚。现在我真的很想知道如何让它工作。

标签: pythonhtmlseleniumweb-scrapingpycharm

解决方案


首先,您使用什么语言?通过进口组织。如果我没记错的话是java,那么你使用的是python。

要在 python 中查找元素,只需:

from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://you_site')
driver.find_element_by_xpath('//div[@class="123 456 789 100"]')

尝试这个,


推荐阅读