猿问

Python selenium 如何检查特定的 web 元素是否包含特定的关键字?

尝试检查页面上是否有特定的 div 类:


<div class="css-1dbjc4n">

包含特定关键字,例如“instagram.com”


我到目前为止的代码是:


Search = driver.find_elements_by_xpath("//*[contains(text(), 'instagram.com')]")

if len(search) > 0:

    print("Found what I was looking for")

上面代码的问题是它搜索整个页面,我需要它只在一个特定的网络元素中搜索。


慕的地6264312
浏览 387回答 3
3回答

PIPIONE

您只需更改正在使用的 XPath 即可完成此操作!search = driver.find_elements_by_xpath("//div[@class='css-1dbjc4n']//div[contains(text(), 'instagram.com')]")if len(search) > 0:&nbsp; &nbsp; print("Found what I was looking for")

慕桂英4014372

您可以使用 WebElement.find_Emement_by_xpath 方法。如下图。ele = driver.find_element_by_class_name("css-1dbjc4n") #Webelement for divresult = ele.find_elements_by_xpath(".//*[text()='instagram.com']")if len(result)>0:&nbsp; &nbsp; print('You found total {} occurance'.format(len(result)))现在,它将在带有类的 div 中打印带有文本instagram.com的元素总数css-1dbjc4n

蝴蝶不菲

driver.find_elements_by_xpath("//*[contains(text(), 'My Button')]")
随时随地看视频慕课网APP

相关分类

Python
我要回答