如何使用预期条件检查 HTML 中是否存在 br
标记。这是 HTML 代码:
<br>
<a href="member.php?u=12455" rel="nofollow">Smatta</a>
<a>, </a>
<a href="member.php?u=14305" rel="nofollow">Nyunyu</a>
<a>, </a>
<a href="member.php?u=20892" rel="nofollow">moyo</a>
<a>, </a>
<a href="member.php?u=21040" rel="nofollow">Masikini_Jeuri</a>
<a>, </a>
<a href="member.php?u=27429" rel="nofollow">Job K</a>
<a>, </a>
<a href="member.php?u=38124" rel="nofollow">Adoe</a>
<a>, </a>
<a href="member.php?u=39196" rel="nofollow">enhe</a>
<a></a>
这是我的代码。
wait = WebDriverWait(browser, 10)
wait.until(EC.visibility_of_element_located((By.XPATH, '//<br>')))
完整代码。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.PhantomJS()
browser.maximize_window()
browser.get("http://www.jamiiforums.com/kenyan-news/225589-kenyan-and-tanzanian-surburbs.html")
username = browser.find_element_by_id("navbar_username")
password = browser.find_element_by_name("vb_login_password_hint")
username.send_keys("MarioP")
password.send_keys("codeswitching")
browser.find_element_by_class_name("loginbutton").click()
wait = WebDriverWait(browser, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, '//h2[contains(., "Redirecting")]')))
wait.until(EC.title_contains('Kenyan & Tanzanian'))
wait.until(EC.visibility_of_element_located((By.ID, 'postlist')))
browser.find_element_by_xpath('//div[@class="vbseo_liked"]/a[contains(@onclick, "return vbseoui.others_click(this)")]')
browser.find_element_by_class_name("vbseo_liked").click()
wait.until(EC.visibility_of_element_located((By.TAG_NAME, 'br')))
print (browser.page_source)
print 'success!!'
browser.close()
我这样做的原因是因为 br 标签只在链接被点击后出现。我试图在单击操作之后获取页面源,但它在单击操作之前为我提供了页面源。我想在获取页面源之前检查 br 标记是否存在。
这是它打印出来的错误。
File "sele.py", line 29, in wait.until(EC.visibility_of_element_located((By.TAG_NAME, 'br')))
最佳答案
只是你的XPath表达式不正确,应该是:
//br
或者,您可以使用“标记名称”定位器:
wait.until(EC.visibility_of_element_located((By.TAG_NAME, 'br')))
作为旁注,我不确定您的意图是什么,但我从未见过有人会在测试自动化或网络中依赖 br
标签的情况-刮。这并不一定意味着您做错了什么,但请确保有充分的理由。
关于python - Selenium 的预期条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29361133/