John Taylor
John Taylor

Reputation: 737

Python Xpath conundrum. I can select the link, try to click, no go

My xpath identifies the link I want to click, in code, the clicking does nothing.

Tried various ways of landing on the link, they all work, still, the click does not.

Here is the xpath that gets me to the link in the html below:

driver.find_element_by_xpath("//span[text()='2009 CR 00203']/ancestor::a").click()

I am expecting the click to work and travel to a new page which is what happens when manually accessing the website like a typical visitor.

Here is the relevant HTML:

<td id="grid$row:47$cell:3" class="bookmarkablePageLinkPropertyColumnLink">
    <span>
    <!-- $Id: BookmarkablePageLinkPanel.html,v 1.2 2015/07/17 21:22:42 zcarter Exp $ -->
        <a href="?x=oETOKPmVudMuqONlkX1Tiv3Gtb5F5sedgQTMNLZ3tcAonXM5HcVTKxw7e-*sHrZvlNHuylIDXkrxaTtpmME4HA" id="grid$row:47$cell:3$link" onclick="$('#processingDialog').dialog('open');">
            <i id="id1d6"></i>
            <span>2009 CR 00203</span>
        </a>
    </span>
</td>

Just to clarify, the actual code is intended to iterate through a list of case numbers. So it is actually this code: (Ignore formatting, couldn't get it to look as it does in my code, formatting wise).

for case in case_lst:
    driver.find_element_by_xpath("//span[text()=case]/ancestor::a").click()
    # grab data from resulting page after click
    driver.back() # returning to previous page with more case links....

Upvotes: 0

Views: 42

Answers (2)

JeffC
JeffC

Reputation: 25734

One issue with your code is that you are using the case variable inside your locator but it's being interpreted as a string "case".

"//span[text()=case]/ancestor::a"

should be

"//span[text()=" + case + "]/ancestor::a"

The updated code should look like

for case in case_lst:
    driver.find_element_by_xpath("//span[text()=" + case + "]/ancestor::a").click()
    # grab data from resulting page after click
    driver.back() # returning to previous page with more case links....

If that gives you an error that the element can't be found, it's likely that the element is inside of an IFRAME or that you need to add a wait for clickable to make sure that part of the page is loaded. Links to the docs for those two cases are below.

docs for switching to an IFRAME

docs for waiting for clickable

Upvotes: 0

supputuri
supputuri

Reputation: 14145

Try javascript click.

ele = driver.find_element_by_xpath("//span[text()='2009 CR 00203']/ancestor::a")
driver.execute_script("arguments[0].click();",ele)

Upvotes: 1

Related Questions