简体   繁体   English

即使使用 Selenium 和 Python 有多个具有相同类名的元素,如何通过类名识别元素

[英]How to identify an element through classname even though there are multiple elements with the same classnames using Selenium and Python

<div class="_2S1VP copyable-text selectable-text" data-tab="1" dir="ltr" spellcheck="true" contenteditable="true"></div>

<div class="_2S1VP copyable-text selectable-text" data-tab="3" dir="ltr" contenteditable="true"></div>

I'm a beginner and I've had a hard time distinguishing / specifying the first class over the second one我是初学者,我很难区分/指定第一个 class 而不是第二个

typing = bot.find_element_by_xpath('//div[@class = "_1Plpp"]')

this doesn't seem to work and just using the class name always brings up the unwanted second one with the same class name, I've noticed that it has data-tab="3" and the other one has data-tab="1" how would i specify the one with data-tab="1" over the other one.这似乎不起作用,仅使用 class 名称总是会弹出不需要的第二个具有相同 class 名称的名称,我注意到它具有 data-tab="3" 而另一个具有 data-tab=" 1" 我将如何指定一个 data-tab="1" 而不是另一个。

As the class attribute of both the elements contains similar values, you won't be able to distinguish them only through class attribute and you may have to consider the some other attribute(s).由于两个元素的class属性包含相似的值,您将无法仅通过class属性来区分它们,您可能必须考虑其他一些属性。

To identify the first element you can use either of the following Locator Strategies :要识别第一个元素,您可以使用以下任一Locator Strategies

  • Using css_selector along with data-tab attribute:使用css_selectordata-tab属性:

     typing = bot.find_element_by_css_selector("div.copyable-text.selectable-text[data-tab='1']")
  • Using xpath along with data-tab attribute:使用xpathdata-tab属性:

     typing = bot.find_element_by_xpath("//div[contains(@class, 'copyable-text') and @data-tab='1']")
  • Using xpath along with data-tab attribute:使用xpathdata-tab属性:

     typing = bot.find_element_by_xpath("//div[contains(@class, 'selectable-text') and @data-tab='1']")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Selenium 选择一个 ClassName,其中有多个同名的 ClassName - Selecting a ClassName using Selenium, where there are multiple ClassNames with the same name 如何使用 Selenium 和 Python 定位具有多个类名的元素 - How to locate an element with multiple classnames using Selenium and Python 如何通过 Selenium 和 Python 使用 classname 属性定位最后一个 web 元素 - How to locate the last web element using classname attribute through Selenium and Python 如何使用 Selenium 和 Python 识别嵌套元素 - How to identify the nested element using Selenium and Python 如何使用 Selenium 和 Python 识别元素 - How to identify the element using Selenium and Python 使用Selenium和python,即使元素存在但实际上不可见,如何检查 - Using selenium and python, how to check that even though an element exists but it is not actually visible 当元素包含多个类名时,如何在 selenium 中复制标签? - How to copy tags in selenium when element contains multiple classnames? 使用selenium python按类名查找第n个元素 - Find nth element by classname using selenium python 如何使用 Selenium 通过 xpath 识别动态输入复选框元素 - How to identify a dynamic input checkbox element through xpath using Selenium 如何在使用beautifulsoup和selenium的Python Scraping中识别classname或id - How to identify the classname or id in Python Scraping with beautifulsoup and selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM