Python 硒的预期条件

Python 硒的预期条件,python,html,selenium,selenium-webdriver,web-scraping,Python,Html,Selenium,Selenium Webdriver,Web Scraping,如何使用预期条件检查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=20

如何使用预期条件检查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>
我这样做的原因是,br标记仅在单击链接后出现。我试图在单击操作之后获取页面源,但它在单击操作之前为我提供了页面源。在获取页面源代码之前,我想检查br标记是否存在

这是它打印出来的错误

文件“sele.py”,第29行,在 等待.until(位于((By.TAG\u NAME,'br'))的元素的EC.visibility\u)


只是XPath表达式不正确,应该是:

//br
或者,您可以使用“标记名”定位器:



顺便说一句,我不确定你的意图是什么,但我从未见过有人会依赖测试自动化或web抓取中的
br
标记。这并不一定意味着你做错了什么,但要确保你有充分的理由这样做。

谢谢你的回答。我这样做的原因是,br标记仅在单击链接后出现。我试图在单击操作之后获取页面源,但它在单击操作之前为我提供了页面源。在获取页面源代码之前,我想检查br标记是否存在。我编辑了这个问题以包含我的代码。@user3078335好的,谢谢,如果你使用
元素的存在性而不是元素的可见性,那该怎么办。我会试试看。我可以检查在特定的div类下是否存在br标记吗?例如,如果br标记出现在此div->//div[@class=“vbseo_liked”]下,而不是单击链接后的整个页面源代码。@user3078335是的,您可以将
By.XPATH
与以下XPATH表达式一起使用:
//div//div[@class=“vbseo_liked”]//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
wait.until(EC.visibility_of_element_located((By.TAG_NAME, 'br')))