selenium 通过 xpath 查找元素和子标签不起作用

问题描述 投票:0回答:1

对于 DOM

<div class="col-md-6">
   <div class="refreshIcon refreshFiling">
      <a href="javascript:;" onclick="CFRefresh('CFanncEquity')"><img src="/assets/images/refresh-icon.svg" alt="Refresh" title="Refresh">Refresh</a>
   </div>
</div>

用我的Python selenium 代码

  refresh_button = driver.find_element(By.XPATH, '//div[@class="refreshIcon"]/a')

我得到了

    selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class="refreshIcon"]/a"}
  (Session info: chrome=126.0.6478.126); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception

为什么?

selenium-webdriver selenium-chromedriver
1个回答
0
投票

根据你给的DOM,xpath应该是:

//div[contains(@class, "refreshIcon")]/a

//div[@class="refreshIcon refreshFiling"]/a
© www.soinside.com 2019 - 2024. All rights reserved.