使用 selenium 和 Python 时,如何设置等待条件来检查没有一个 div 元素具有包含特定字符串的 ID?

发布于 2025-01-17 08:19:39 字数 1303 浏览 0 评论 0原文

这是xpath的一个示例,以计算元素 $ x(“ count(// div [contains(text(),'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)

我遇到此错误 无效的Selectorexception:消息:给定的XPath表达式“ Bolean(// c count(contains(text(),'domain')))== 1)” == 1)”是无效的:syntaxerror:document.evaluate:eversevaluate:表达式不是一个表达式法律表达

我希望一旦上述工作,我就可以将其变成负面搜索(count == 0),因此主题行是我所追求的

This is an example of XPATH to count the elements
$x("count(//div[contains(text(),'domain')])") ◄ you can test this in the browser
The below code should work if the wait condition is satisfied (find a "p" tag where the text contains "domain".

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)

I am getting this error
InvalidSelectorException: Message: Given xpath expression "bolean(count(//p[contains(text(), 'domain')])==1)" is invalid: SyntaxError: Document.evaluate: The expression is not a legal expression

I would expect that once the above works I can turn that into a negative search (count==0) hence the subject line which is what I am after

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咆哮 2025-01-24 08:19:39

要等待所有具有 ID 的

元素不包含特定字符串,您需要引入 WebDriver等待 invisibility_of_element_ located() 并且您可以使用以下任一定位策略

  • 使用xpath

    WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.XPATH, "//div[contains(@id, 'domain')]")))
    
  • 注意:您必须添加以下导入:

    从 selenium.webdriver.support.ui 导入 WebDriverWait
    从 selenium.webdriver.common.by 导入
    从 selenium.webdriver.support 导入预期条件作为 EC
    

参考文献

您可以在以下位置找到相关的详细讨论:

To wait for all the <div> elements with an ID not to contain a certain string you need to induce WebDriverWait for the invisibility_of_element_located() and you can use either of the following locator strategies:

  • Using xpath:

    WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.XPATH, "//div[contains(@id, 'domain')]")))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

References

You can find a relevant detailed discussion in:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文