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! 😊