这就是我所拥有的
for arrow in self.driver.find_elements_by_xpath("//img[contains(@src, 'disclosure_closed.gif')]/"):
arrow.click()但它把这个扔给了我
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //img[contains(@src, 'disclosure_closed.gif')]/ because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//img[contains(@src, 'disclosure_closed.gif')]/' is not a valid XPath expression.
(Session info: chrome=42.0.2311.135)
(Driver info: chromedriver=2.15.322448 (52179c1b310fec1797c81ea9a20326839860b7d3),platform=Windows NT 6.1 SP1 x86_64)我也试过这样做:
image_url = "http://example.com/disclosure_closed.gif"
for arrow in self.driver.find_elements_by_xpath("//img[@src='%s']/" %(image_url)):
arrow.click()这是html:
<img align="top" style="cursor:pointer;" onclick="disclose('44');" id="44disclosure" src="http://example.com/disclosure_closed.gif">发布于 2015-05-02 16:54:42
删除xpath末尾的/。
发布于 2015-05-02 21:06:00
看来你已经得到答案了。但是,您不需要为该标识符使用xpath。xpaths比css慢,而且(通常)也不可靠。您可以使用以下css选择器来标识相同的元素:
[src*='disclosure_closed.gif']或者(特别是用于图像)
img[src*='disclosure_closed.gif']https://stackoverflow.com/questions/30004649
复制相似问题