0

Got this issue: I'm trying to find the last link on webpage:

    try:
            elems=self.br.find_elements_by_partial_link_text(category)
    except NoSuchElementException:
            print 'The Category Name Was Not Found'.category
            f=open("error.log","aw")
            f.write("Cant find category:%s\n"%category)
            f.close()
    elems[-1].click()

Now for exmaple the categories I have: Books (Dynamic Number) AudioBooks (Dynamic Number) RomanceBooks (Dynamic Number)

And I try to find "Books" I want to get the first one! (Also it needs to be the last on the source so can't choose first from the elements) One thing that will work with my system if I was able to choose partial_link_text starting with books and not ending? Any ideas? I can't use any other method because It's a really dynamic code and most of the stuff changes...

7
  • Could you please clarify a little - you want to get the last of the elements (based on your elems[-1]) but you say you want the first one - the first of the list you've made or the first of something else? Commented Jul 23, 2013 at 14:42
  • @MarkRowlands I want to get the last element (elems[-1]) but it must be starting with my category variable (Books) so it will choose the one that is starting with category variable and not ending so basally the last one that start with category (Books) Commented Jul 23, 2013 at 15:01
  • Thanks, I think I understand a little more now. Sorry, another question first: So is the problem that you use "Books" as your category but instead of just providing a list of "Books" its also including "AudioBooks" and "RomanceBooks" because of the _partial_link_text? Commented Jul 23, 2013 at 15:06
  • @MarkRowlands Yes exactly.But also I need the last one from The Books Links matches because there is few links with same category before. I need the last match of Books and it must start with Books or it should not be in the list Commented Jul 23, 2013 at 15:38
  • Due to the problems presented by the _partial_link_text also returning elements from the incorrect categories is it perhaps not the best choice of locator in this instance? Commented Jul 23, 2013 at 15:46

1 Answer 1

1

You can use something like this

xpath="//a[starts-with(text(), %s)]" % category
elems = self.br.find_elements_by_xpath(xpath)
elem = elems[-1]

xpath will do the starts-with part of work, and elems[-1] will do the rest

Sign up to request clarification or add additional context in comments.

3 Comments

it's a checkbox I'm trying to click here is the html code do you think it will work for this? <input class="l-tcb" type="checkbox" id="ext-gen23"> <a hidefocus="on" href="#" id="ext-gen16"><span unselectable="on">Books (206)</span></a>
also start with is basically category
no, this example won't work. but you can play with xpath. for example, if each pair of input and a is contained in some node (like in div or anything else) you can just use a bit another xpath, like this: xpath="//a[starts-with(text(), %s)]/../input" % category, and you'll get the checkbox you want.

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.