4

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')))

user3078335
  • 781
  • 4
  • 13
  • 24

1 Answers1

4

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.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thanks for your response. 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. I edited the question to include my code. – user3078335 Mar 31 '15 at 14:40
  • 1
    @user3078335 okay, thanks, what if you use `presence_of_element_located` instead of `visibility_of_element_located`? – alecxe Mar 31 '15 at 14:50
  • can i check if there is the presence of the br tag under a specific div class? For instance if the br tag occurs under this div -> //div[@class="vbseo_liked"] and not the entire page source after a link is clicked. – user3078335 Mar 31 '15 at 14:55
  • @user3078335 yeah, you can use `By.XPATH` with the following xpath expression: `//div//div[@class="vbseo_liked"]//br`. – alecxe Mar 31 '15 at 14:59
  • Your edit was supposed to work but it didn't. Turns out the page source never updates after the link is clicked. I only see the br tag in the developer source, but when i check the page source, the br tag never appears. Pretty sure it's javascript related.. I will have to look into it further. Thanks again for your help! – user3078335 Mar 31 '15 at 18:26