当前位置: 首页 > 知识库问答 >
问题:

为什么selenium不能从我的XPath中找到这个看似简单的元素?

王俊哲
2023-03-14
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>

下面是我得到的错误消息:

Traceback(最近的调用为last):文件“/users/joshsong/desktop/liking Your friend's photos.py”,第33行,在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))文件“f.find_element(By=by.xpath,value=xpath)文件”/library/frameworks/python.framework/versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py“,第978行,在find_element”value“:value}中)['value']文件”/library/frameworks/python.framework/remote/webdriver.py“,第321行,在execute lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py”,第242行,check_response引发exception_class(message,screen,stacktrace)selenium.common.exceptions.nosuchelementexception:message:没有这样的元素:找不到元素:{“method”:“xpath”,“selector”:“//[@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)

共有1个答案

陶博涉
2023-03-14

我们需要更多的HTML来充分回答这个问题。例如,对于您所提供的,一个解决方案是:

like_button = driver.find_element_by_xpath("//span[@class='FY9nT fr66n']/button")
like_button.click()

基于提供的HTML,它将单击第一个按钮(实际上是与您提供的不同的按钮,但我假设Like按钮在那里)。如果您想要的每个类似按钮周围的span元素都具有相同的类,则只需要进行较小的修改:

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()

假设每个Like按钮周围的span元素是相同的,并且该类不会在页面上没有包装Like按钮的其他span元素上重用,此解决方案应该可以工作。如果您更新了HTML,并且违反了这些假设,我很乐意查看一个新的解决方案

 类似资料:
  • 问题内容: 我正在尝试编写Selenium测试用例,并从Firefox中的Selenium IDE开始。那里的测试工作很好。现在,我想使用Selenium Webdriver自动化该测试,并导出相应的JAVA类。到目前为止,一切都已设置并且运行良好(Internet Explorer窗口正在打开,显示相应的页面)。 但是:Selenium Webdriver找不到元素。我想获得以下元素: 而且我有

  • Selenium.Common.Exceptions.NosuChelementException:消息:没有这样的元素:找不到元素:

  • 我试图找到这个按钮并点击它。但我得到了这个错误: 我注意到它正在寻找不同的字符串bc它在类名前面添加了这个点。这是问题吗? 非常感谢。

  • 我正在尝试使用python selenium脚本单击gmail主页右上角的“登录”标签。我使用firebug/firepath为这个类找到了正确的xpath,在使用浏览器工具时,它似乎工作得很好,但当脚本尝试使用xpath查找相同的元素时,它失败了。如果你能给我指出正确的方向,我将不胜感激。非常感谢。 Url:https://www.google.com/gmail/about/ 附言:我对硒比较

  • 问题内容: 有人可以发现我的错误吗,这应该是SQL中的合法查询吗? ON子句中的未知列u.usr_auto_key 这是数据库模式: 这是查询… 问题答案: 尝试切换,并在from子句中:

  • 问题内容: 我有以下XPath: 当我在XPath Checker(Firefox扩展)中试用此XPath时,它一直都能完美运行。但是当我在Selenium中执行以下操作时: 它不断给我以下日志错误: 我为解决这个问题而疯狂。有人看到我的代码行有任何错误吗? 问题答案: 该查询字符串不应该像这样(根据javadoc api)吗?