Python 为什么可以';selenium从我的xpath中找不到这个看似简单的元素吗?
我对Selenium还不太熟悉,但这只虫子真让我讨厌! 我正在尝试编写一个机器人程序,在Instagram订阅源的前10张照片上单击“喜欢”按钮。我刚刚从Chrome复制并粘贴了like按钮的XPath,但由于某些原因,此XPath无法工作 代码如下:Python 为什么可以';selenium从我的xpath中找不到这个看似简单的元素吗?,python,html,selenium,xpath,Python,Html,Selenium,Xpath,我对Selenium还不太熟悉,但这只虫子真让我讨厌! 我正在尝试编写一个机器人程序,在Instagram订阅源的前10张照片上单击“喜欢”按钮。我刚刚从Chrome复制并粘贴了like按钮的XPath,但由于某些原因,此XPath无法工作 代码如下: from selenium import webdriver from selenium.webdriver.common.keys import Keys from time import sleep, strftime from random
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep, strftime
from random import randint
sleep(3)
for x in range(1,10):
button_like = webdriver.find_element_by_xpath('//*[@id="react-root"]/section/main/section/div[2]/div[1]/div/article[{}]/div[2]/section[1]/span[1]/button/span'.format(x))
button_like.click()
sleep(randint(2,3))
这是这个部分的HTML,还有我想点击的按钮
<section class="ltpMr Slqrh">
<span class="FY9nT fr66n">
<button class="dCJp8 afkep _0mzm-">
<span class="glyphsSpriteHeart__filled__24__red_5 u-__7" aria-label="Unlike"></span>
</button>
</span>
<span class="_15y0l">
<button class="dCJp8 afkep _0mzm-">
<span class="glyphsSpriteComment__outline__24__grey_9 u-__7" aria-label="Comment"></span>
</button></span>
<span class="_5e4p">
<button class="dCJp8 afkep _0mzm-">
<span class="glyphsSpriteShare__outline__24__grey_9 u-__7" aria-label="Share Post"></span>
</button>
</span>
<span class="wmtNn">
<button class="dCJp8 afkep _0mzm-">
<span class="glyphsSpriteSave__outline__24__grey_9 u-__7" aria-label="Save"></span>
</button>
</span>
</section>
下面是我收到的错误消息:
回溯(最近一次呼叫最后一次):
文件“/Users/JoshSong/Desktop/Liking Your Friend's Photos.py”,第33行,在
button_like=webdriver。通过xpath('/[@id=“react root”]/section/main/section/div[2]/div[1]/div/article[{}]/div[2]/section[1]/span[1]/button/span.查找元素。格式(x))
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/selenium/webdriver/remote/webdriver.py”,第394行,在find_element_by_xpath中
返回self.find_元素(by=by.XPATH,value=XPATH)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/selenium/webdriver/remote/webdriver.py”,第978行,在find_元素中
'value':value})['value']
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/selenium/webdriver/remote/webdriver.py”,执行中第321行
self.error\u handler.check\u响应(响应)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/selenium/webdriver/remote/errorhandler.py”,第242行,在check_响应中
引发异常类(消息、屏幕、堆栈跟踪)
selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素:{“方法”:“xpath”,“选择器”:“/[@id=“react root”]/section/main/section/div[2]/div[1]/div/article[1]/div[2]/section[1]/span[1]/button/span}
(会话信息:chrome=76.0.3809.100)
在尝试使用带索引的路径时,我也遇到了类似的问题。我建议使用这个://span[@aria label='Like']。我没有在python中使用selenium,但我假设有一个find方法,它将索引作为参数。所以你可以用它来点击第一个like元素,然后点击第二个,依此类推(但从instagram网站上的情况来看,一次只能检测到7个)我在尝试使用带有索引的路径时也遇到了类似的问题。我建议使用这个://span[@aria label='Like']。我没有在python中使用selenium,但我假设有一个find方法,它将索引作为参数。因此,您可以使用它来点击第一个like元素,然后点击第二个,依此类推(但从instagram网站上看起来,一次只能检测到7个元素)我们需要更多的HTML才能充分回答这个问题。例如,根据您提供的,一种解决方案是:
like_button = driver.find_element_by_xpath("//span[@class='FY9nT fr66n']/button")
like_button.click()
根据所提供的HTML,这将单击第一个按钮(这实际上与您提供的按钮不同,但我假设Like按钮就在那里)。如果您想要的每个类似按钮周围的跨度元素具有相同的类,则只需稍作修改:
like_buttons = driver.find_elements_by_xpath("//span[@class='FY9nT fr66n']/button")
#create maxIndex of the lesser of the length, or the max index you want to press
maxIndex = MIN(9, len(like_buttons))
for i in range (0, maxIndex):
like_buttons[i].click()
假设围绕每个类似按钮的span元素是相同的,并且该类不会在页面上不包装类似按钮的其他span元素上重用,则此解决方案应该可以工作。如果您更新了HTML,并且违反了这些假设,我很乐意回顾一个新的解决方案我们需要更多的HTML才能充分回答这个问题。例如,根据您提供的,一种解决方案是:
like_button = driver.find_element_by_xpath("//span[@class='FY9nT fr66n']/button")
like_button.click()
根据所提供的HTML,这将单击第一个按钮(这实际上与您提供的按钮不同,但我假设Like按钮就在那里)。如果您想要的每个类似按钮周围的跨度元素具有相同的类,则只需稍作修改:
like_buttons = driver.find_elements_by_xpath("//span[@class='FY9nT fr66n']/button")
#create maxIndex of the lesser of the length, or the max index you want to press
maxIndex = MIN(9, len(like_buttons))
for i in range (0, maxIndex):
like_buttons[i].click()
假设围绕每个类似按钮的span元素是相同的,并且该类不会在页面上不包装类似按钮的其他span元素上重用,则此解决方案应该可以工作。如果您更新了HTML,并且违反了这些假设,我很乐意查看一个新的解决方案试试这个/*[@id=“react root”]/section/main/section/div[2]/div/article/div[2]/section[1]/span[1]/button/spanah,它不幸地遇到了同样的错误。您需要共享上面按钮的HTML。你想让我分享更多的html吗?是的,从section/main开始尝试这个//*[@id=“react root”]/section/main/section/div[2]/div[1]/div/article/div[2]/section[1]/span[1]/button/spanah,不幸的是,你需要分享xml/htmlI共享上面按钮的html。你想让我分享更多的html吗?是的,从section/main这是迄今为止唯一有效的解决方案!但是,它单击带有aria标签=‘Like’的所有内容,包括注释。我不想点击评论,只想点击主帖子。评论的类别与主帖子不同。有没有什么方法可以让我按类区分XPath,这样我就可以点击主帖子而不是评论了?问题解决了!非常感谢您的光临!我使用的行是buttons=webdriver.find_elements_by_xpath(//span[contains(@class,'GlyphsPriteHeart,'uuu大纲,'uuuu 24'uu灰色,'uuu 9'))和contains(@aria label,'Like')),很高兴你找到了它@JoshuaSong,很抱歉,我没能足够快地回答你的第一个评论。不,你让我走上了正确的道路,没有你我是不可能做到的。希望你有一个美好的一周!这是迄今为止唯一有效的解决方案!但是,它单击带有aria标签=‘Like’的所有内容,包括注释。我不想点击评论,只想点击主帖子。评论的类别与主帖子不同。有没有wa