1

I have created a list of elements matching an xpath and would like to click through each successively. However, if I use the get_attribute("href") command I get a 'unicode' object has no attribute 'click' error. This is because href is a string. If I don't use get_attribute and simply use this command:

driver.find_elements_by_xpath(".//div/div/div[3]/table//tr[12]/td/table//tr/td/a")

I get a list full of elements. I can successfully click on the first link in the list; however, when I click on the second I get this error: 'Element not found in the cache - perhaps the page has changed since it was looked up' I imagine that the reason that the page links I am trying to iterate through are generated via a search query into java (this is one of the href links:

javascript:__doPostBack('ctl00$Content$listJobsByAll1$GridView2','Page$3') )

One more piece of relevant information: there are only two attributes at this xpath location: href and the text.

So, given that I am dealing with a java website and only the two attributes, I am hoping someone can tell me which webdriver commands I can use to get a series of clickable static links. Beyond a specific answer, any advice on how I could have figured this out myself would be helpful.

1 Answer 1

2

if you click on a link with selenium, you are changing the current page. The page that you are directed to doesn't have the next element.

to get links use:

'.//tag/@href'

you can try:

for elem in elems:
    elem.click()
    print browser.current_url
    browser.back()
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I don't know why I didn't try that already- no reason why it wouldn't work. I'll give it a run when I get home.
So when I went back the elements were still different, thus I had to 1) count the elements, then 2) Iterate through the range of the element list, and instantiate the list every time because clicking on the i'th element.
for i in driver.find_elements_by_xpath(".//div/div/div[3]/table//tr[12]/td/table//tr/td/a"): list.append(i) count = len(list)-1 print count for i in range(count): print i target = driver.find_elements_by_xpath(".//div/div/div[3]/table//tr[12]/td/table//tr/td/a") print target target[i].click() time.sleep(5) driver.back() time.sleep(5)
@user1780852, if the answer is correct and helpful to you, then you should accept it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.