python - using child nodes as qualifiers in xpath, for selenium webdriver -


i using selenium webdriver in django (python) app. question xpath however.

for reason, schools have not standardized central source lookup transcript information, , thoroughly enjoy charging former students wonderful service of clicking button, while requiring paying party work of ordering them. automating ordering them , came across recent pattern function wrote can't handle:

<a href="https://hccadvisor.hccfl.edu:443/webadvisor/webadvisor?tokenidx=4932326730&amp;type=p&amp;pid=ut-lgrq"      onmouseover="window.status=''; return true;">     <span class="label">log in </span> </a> 

it must grabbed each time, token being tacked on in middle.

my function calling

def access_link_by_text(text, driver):     block = driver.find_element_by_xpath("//a[text()='{text}']".format(text=text))     link  = block.get_attribute('href')     driver.get(link) 

but @ 2 of other schools sites see wrapping text within spans:

login_block = hcc_driver.find_element_by_xpath("//a/span[text()='log in ']") login_link  = login_block.get_attribute('href') 

where login_link not exist, login_block not grabbing <a href... want to. grabs span, since said "any a's span children text log in ". want grab a using child span test, tried:

login_block = hcc_driver.find_element_by_xpath("//a/child::span[text()='log in ']") 

which failed.

how use children/parents/siblings qualifying tests, without trying pick them can link?

thank you

where login_link not exist, login_block not grabbing <a href... want to. grabs span, since said "any a's span children text log in ". want grab using child span test, tried:

you close. can nest predicates. solution should be:

//a[child::span[text()='log in ']] 

which select every <a> in document has span has text node text "log in ". may want careful possible added whitespace, bit safer:

//a[child::span[normalize-space(text())='log in']] 

and fun of it, can revert expression, if find more readable:

span[normalize-space(text())='log in']/parent::a 

Comments