预期的硒条件 [英] Expected conditions with selenium
问题描述
如何使用预期条件检查HTML中是否存在 br
标记。这是HTML代码:
< br>
< a href =member.php?u = 12455 =nofollow> Smatta< / a>
< a>,< / a>
< a href =member.php?u = 14305 =nofollow> Nyunyu< / a>
< a>,< / a>
< a href =member.php?u = 20892 =nofollow> moyo< / a>
< a>,< / a>
< a href =member.php?u = 21040 =nofollow> Masikini_Jeuri< / a>
< a>,< / a>
< a href =member.php?u = 27429 =nofollow>作业K< / a>
< a>,< / a>
< a href =member.php?u = 38124 =nofollow> Adoe< / a>
< a>,< / a>
< a href =member.php?u = 39196 =nofollow> enhe< / a>
< a>< / a>
这是我的代码。
<$ p $ ($ .C $ c $> wait = WebDriverWait(browser,10)
wait.until(EC.visibility_of_element_located((By.XPATH,'//< br>)))
完整的代码。
从selenium导入webdriver
来自selenium.webdriver.common.by导入通过selenium.webdriver.support.ui获取
从selenium.webdriver.support导入WebDriverWait
导入expected_conditions作为EC
browser = webdriver.PhantomJS()
browser.maximize_window()
browser.get(http://www.jamiiforums.com/kenyan-news/225589-kenyan-and-tanzanian )
username = browser.find_element_by_id(navbar_username)
password = browser.find_element_by_name(vb_login_password_hint)
username.send_keys( (button)。
password.send_keys( 20)
wait.unti (EC.visibility_of_element_located((By.XPATH,'// h2 [contains(。,Redirecting)]')))
wait.until(EC.title_contains('Kenyan&坦桑尼亚'))
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标签的存在。
这是打印出的错误。
在
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
标签。这并不意味着你做错了什么,但要确保有一个坚实的理由。
How do i check if a br
tag is present in HTML using expected conditions. This is the HTML code:
<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>
This is my code.
wait = WebDriverWait(browser, 10)
wait.until(EC.visibility_of_element_located((By.XPATH, '//<br>')))
Full code.
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()
The reason I am doing that is because the br tag only appears after a link has been clicked. I am trying to get the page source after the click action, but it gives me the page source before the click action. I wanted to check the presence of the br tag before i get the page source.
This is the error it prints out.
File "sele.py", line 29, in
wait.until(EC.visibility_of_element_located((By.TAG_NAME, 'br')))
解决方案 It's just that your XPath expression is incorrect, should be:
//br
Or, you can use the "tag name" locator:
wait.until(EC.visibility_of_element_located((By.TAG_NAME, 'br')))
As a side note, I'm not sure what your intentions are, but I've never seen a case where someone would rely on a presence of a br
tag in test automation or web-scraping. It doesn't necessarily mean you are doing something wrong, but make sure there is a solid reason for that.
这篇关于预期的硒条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!