使用 selenium 和 Python 时,如何设置等待条件来检查没有一个 div 元素具有包含特定字符串的 ID?
这是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 errorInvalidSelectorException: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要等待所有具有 ID 的
元素不包含特定字符串,您需要引入 WebDriver等待 invisibility_of_element_ located() 并且您可以使用以下任一定位策略:
使用xpath:
注意:您必须添加以下导入:
参考文献
您可以在以下位置找到相关的详细讨论:
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:
Note : You have to add the following imports :
References
You can find a relevant detailed discussion in: