XPATH for Dummies

I just found a new trick for using the contains command in xpath. When you want to find two separate strings that occur in an attribute of a tag.  You just write two different contains and use and/or in between.

Example:

url = html.xpath('//li[contains(@class,"-navigation") and contains(@class,"-right")]/a//@href')[0]

The example above will look for a

  • tag where class contains the strings '-navigation' and '-right'. This also works with or.

    One thing I found is that there can be no overlap in the characters used in each strings. In the example above, I first tried to use:

    '-navigation-'

    but found that it did not work because in some cases within the code of the web pages I was trying to access, the two strings overlapped and the class attribute of the

  • tag contained:

     '-navigation-right'

    It did not work in my code until I changed the first contain statement to '-navigation' to avoid any overlaps