I have an html file with links and would like to distinguish if a link is an image or if a link is text. I am using Python 3 and Selenium with Firefox or PhantomJS browser. The goal is to have an automatic procedure to go through several hundred html files and find links with images.
What I do: I first focus only on one html file. In a first step I get the names of all images in the html file, where I know that one image is a link and the others are not. I call the images IMAGENAME.
Then I try to derive if one or more of the images is a link. I use:
driver.find_element_by_xpath('(//a[/img[contains(text(),"'+IMAGENAME[j]+'")]])')
or
driver.find_element_by_xpath('//a[/img[contains(@src,IMAGENAME[j])]]')
or
driver.find_element_by_xpath('//a/img[contains(@src,IMAGENAME[j])]')
with j = 0 (image is not a link) and j = 1 (image is a link).
In each case I get the same error message, which tells me that the way I call the image must be wrong:
NoSuchElementException: Message: no such element: Unable to locate element:...
When I leave the //a part out and use only //img, then I get all images without error message.
What am I doing wrong in calling the image in a link? Is there another way I can do this?
//a[img]will return you links with images and//a[not(img)]- links with text. Note that if you want to usejvariable inXPathyou should use'xpath expression with %s variable' % j, but not'xpath expression with j variable'\\astands for anchor, whereas you might be looking forhref?