【发布时间】:2021-06-21 15:34:14
【问题描述】:
我想检查浏览器是否能够查找包含不同单词的许多不同元素的存在。 在这种情况下,我有时会使用此代码:
try:
elemtwo = WebDriverWait(browser, 2, poll_frequency=0.1, ignored_exceptions=None).until(
EC.element_to_be_clickable((By.XPATH, "//*[contains(text(), ' first example of text')]")))
if elemtwo:
break
except NoSuchElementException and TimeoutException:
pass
try:
elem = WebDriverWait(browser, 2, poll_frequency=0.1, ignored_exceptions=None).until(
EC.element_to_be_clickable((By.XPATH, "//*[contains(text(), 'second example of text')]")))
if elem:
break
except NoSuchElementException and TimeoutException:
pass
如果我有 20 个不同的单词,我应该怎么做才能通过尽可能少的书写找到它们? 让我更好地解释一下:例如,我有一个这样的 20 个单词的列表:
- 警察;
- 苹果;
- 猴子;
- ecc...
同一个命令{browser.find_element_by_xpath("//*[contains(text(), "one of the list's word")]")}不写20次怎么办?
FIRST EDIT:对不起,我没有说明以上20个字或文字是我随意选择的,无论它们与网站的相关性如何。
【问题讨论】:
-
我们可以用 Java 做到这一点,但不能用 Python
-
@Prophet:对不起!你说我们不能在 python 中做到这一点是什么意思?
-
在我看来,Prophet意味着在python上我必须写: browser.find_element_by_xpath("//*[contains(text(), "police")]") browser.find_element_by_xpath("/ /*[contains(text(), "apple")]") browser.find_element_by_xpath("//*[contains(text(), "monkey")]") ecc....并对每个单词重复该命令我正在寻找,
-
为什么不将它们存储在所有警察、苹果的列表中,然后使用循环并一一提供给 find_element
-
cruisepandey,我也有类似的想法,但我无法具体化。
标签: python selenium automation