问题
How can I use selenium's find_elements_by_xpath()
based on text, when it may or may not have a word?
Example: it can be either #1 here
or #1 is here
. I want both to be part of the same list, since that func return a list. ATM I have driver.find_elements_by_xpath("//*[contains(text(), '#1 here')]")
but that would only find the first case, not the ones with an is
. Basically, something like driver.find_elements_by_xpath("//*[contains(text(), '#1 here' or '#1 is here')]")
How could I do that?
I'm also trying to keep their "order" So, first one from top to bot is #1 on list, etc
I could do 2 different lists, one for each and then combine them but that would also screw that up
回答1:
You can use the or
clause you can use either of the following xpath based Locator Strategies:
Selenium3:
driver.find_elements_by_xpath("//*[contains(., 'this') or contains(., 'or this')]")
Selenium4:
driver.find_elements(By.XPATH, "//*[contains(., 'this') or contains(., 'or this')]")
来源:https://stackoverflow.com/questions/65659942/seleniums-find-elements-with-two-clauses-for-text