python - Selenium 的预期条件

标签 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=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/

相关文章:

css - 如何在 java 中使用 firebug 访问节点的 CSS 属性

python - 如何在Python中使用selenium跟踪动态更新代码

node.js - Selenium Node + Sauce 实验室测试 : Cannot Finish a Task

html - REACT spa 应用程序 - 为 SEO、优点和缺点提供单独和不同的预渲染静态 html

python - lru_cache 干扰 single_dispatch 完成的类型检查

python - 如何强制 python 忽略或重新创建 pyc/pyo 文件?

python - 如何在 numpy 中获得逐元素矩阵乘法(Hadamard 乘积)?

jQuery HTML Button 多次触发函数

jquery - 除非我指定高度,否则 Div 不会在屏幕上居中

python : Sum of numbers in different files