[英]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_selector
和data-tab属性:
typing = bot.find_element_by_css_selector("div.copyable-text.selectable-text[data-tab='1']")
Using xpath
along with data-tab attribute:使用
xpath
和data-tab属性:
typing = bot.find_element_by_xpath("//div[contains(@class, 'copyable-text') and @data-tab='1']")
Using xpath
along with data-tab attribute:使用
xpath
和data-tab属性:
typing = bot.find_element_by_xpath("//div[contains(@class, 'selectable-text') and @data-tab='1']")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.