【AI问答】

在Selenium中,可以使用By.cssSelector或By.xpath方法进行class的模糊查找。例如,使用CSS选择器的$class*$,$class^$和$class*$来查找以特定字符串结束、开始或包含该字符串的class。XPath表达式如contains(@class,abc)则可用于查找class属性包含特定文本的元素。
摘要由CSDN通过智能技术生成

Question 1:在selenium中模糊查找class

Hello, I would like to use selenium to fuzzily search for a class. What can I do?
In class, only ‘abc’ is known

class=‘abc – as8d7asufaskjhfa’

AI- answer:
Hi, thank you for using Bing. Selenium is a popular tool for automating web browser interactions. Fuzzy search is a technique for finding approximate matches for a given query. If you want to use selenium to fuzzily search for a class, you can try some of the following methods:

You can use the By.cssSelector method to find elements that have a class attribute that ends with, starts with, or contains a certain string. For example, if you want to find elements that have a class attribute that ends with ‘abc’, you can use By.cssSelector(“[class$=abc]”). Similarly, you can use By.cssSelector(“[class^=abc]”) for elements that start with ‘abc’, and By.cssSelector(“[class*=abc]”) for elements that contain ‘abc’. You can also combine these selectors with logical operators like and and or. For more details, see this answer1.
You can use the By.xpath method to find elements that have a class attribute that matches a certain XPath expression. XPath is a language for querying XML documents, and it has many functions and operators for finding partial matches. For example, if you want to find elements that have a class attribute that contains ‘abc’, you can use By.xpath(“//[contains(@class,‘abc’)]“). Similarly, you can use By.xpath(”//[starts-with(@class,‘abc’)]”) for elements that start with ‘abc’, and By.xpath(“//*[ends-with(@class,‘abc’)]”) for elements that end with ‘abc’. You can also use other XPath functions like matches, substring, and translate to perform more complex fuzzy matching. For more details, see this answer2.
You can use the driver.findElements method to find all the elements that have a certain class name, and then iterate over them and compare their text or attributes with your query using Python’s built-in string methods or regular expressions. For example, if you want to find elements that have a class name ‘abc’ and their text contains ‘xyz’, you can use something like this:
elements = driver.findElements(By.className(“abc”))
for element in elements:
if “xyz” in element.text:
# do something with the element
Copy
For more details, see this answer.

I hope this helps you with your selenium project. Have a nice day! 😊

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值