使用 selenium 和 Python 时,如何设置等待条件以检查 div 元素中的 none 是否具有包含特定字符串的 ID?
When using selenium and Python how do I set a wait condition that checks that none of the div elements has an ID that contains a certain string?
这是一个 XPATH 计算元素的例子
$x("count(//div[contains(text(),'domain')])")
◄ 你可以在浏览器中测试这个
如果满足等待条件,下面的代码应该可以工作(找到文本包含“domain”的“p”标签。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
options = webdriver.FirefoxOptions()
options.add_argument("--headless") # Ensure GUI is off
webdriver_service = Service("/home/myuser/geckodriver/geckodriver")
browser = webdriver.Firefox(service=webdriver_service, options=options)
# Get element with tag name 'div'
xpath_count_condition="bolean(count(//p[contains(text(), 'domain')])==1)"
browser.get("https://www.example.com")
wait=WebDriverWait(browser, 3).until(lambda browser:
browser.find_element(by=By.XPATH,value=xpath_count_condition))
element = browser.find_element(By.TAG_NAME, 'lulu')
# Get all the elements available with tag name 'p'
elements = element.find_elements(By.TAG_NAME, 'p')
for e in elements:
print(e.text)
我遇到了这个错误
InvalidSelectorException: Message: Given xpath expression "bolean(count(//p[contains(text(), 'domain')])==1)" is invalid: SyntaxError: Document.evaluate: The expression is not a legal expression
我希望一旦上述工作成功,我就可以将其变成负面搜索(计数==0),因此主题行就是我想要的
要等待 ID 不包含特定字符串的所有 <div>
元素,您需要引入 WebDriverWait for the and you can use either of the following :
使用 xpath:
WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.XPATH, "//div[contains(@id, 'domain')]")))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
参考资料
您可以在以下位置找到相关的详细讨论:
这是一个 XPATH 计算元素的例子
$x("count(//div[contains(text(),'domain')])")
◄ 你可以在浏览器中测试这个
如果满足等待条件,下面的代码应该可以工作(找到文本包含“domain”的“p”标签。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
options = webdriver.FirefoxOptions()
options.add_argument("--headless") # Ensure GUI is off
webdriver_service = Service("/home/myuser/geckodriver/geckodriver")
browser = webdriver.Firefox(service=webdriver_service, options=options)
# Get element with tag name 'div'
xpath_count_condition="bolean(count(//p[contains(text(), 'domain')])==1)"
browser.get("https://www.example.com")
wait=WebDriverWait(browser, 3).until(lambda browser:
browser.find_element(by=By.XPATH,value=xpath_count_condition))
element = browser.find_element(By.TAG_NAME, 'lulu')
# Get all the elements available with tag name 'p'
elements = element.find_elements(By.TAG_NAME, 'p')
for e in elements:
print(e.text)
我遇到了这个错误
InvalidSelectorException: Message: Given xpath expression "bolean(count(//p[contains(text(), 'domain')])==1)" is invalid: SyntaxError: Document.evaluate: The expression is not a legal expression
我希望一旦上述工作成功,我就可以将其变成负面搜索(计数==0),因此主题行就是我想要的
要等待 ID 不包含特定字符串的所有 <div>
元素,您需要引入 WebDriverWait for the
使用 xpath:
WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.XPATH, "//div[contains(@id, 'domain')]")))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
参考资料
您可以在以下位置找到相关的详细讨论: