Skip to main content
 首页 » 编程设计

Python Selenium 选择 div 类

2025年05月04日14zlslch

所以我正在使用 Selenium 编写脚本,但我在寻找 div 类时遇到了困难。

网站上的结构是这样的:

<div class="field first_name"> 
    <input type="text" placeholder="First Name"></input> 
</div>` 

已经尝试过类似driver.find_element_by_xpath("//field[@first_name]") 但由于某种原因它没有起作用,也许我做错了或者不得不以不同的方式。

有人知道吗?

请您参考如下方法:

您正在以不受支持的方式混合类和 xpath 定位器!

以下任一方法都有效:

driver.find_element_by_xpath("//div[contains(@class, 'first_name')]") 
driver.find_element_by_class_name("first_name") 

您可能需要考虑阅读一些 documentation在 XPath 上,也许是 Selenium locators一般而言。