【问题标题】:Can't find element located by By.xpath找不到 By.xpath 定位的元素
【发布时间】:2023-04-01 00:30:01
【问题描述】:

我尝试使用 xpath、类名和 css 定位器单击此按钮。我什至等待了一段时间,以便它可以先正确加载,但没有任何效果。仍然无法点击按钮,请帮助。

<div class="relative create-disbursement-dropdown" size="small">

<button data-v-a7f43302="" type="button" variant="solid" color="green" size="small" lefticon="" righticon="" options="[object Object],[object Object]" class="
    relative
    box-border
    transition-all
    duration-200
    font-sans font-semibold
    rounded-semi
    py-2
    disabled:pointer-events-none
    disabled:cursor-default
    focus:outline-none
   btn-small btn-solid btn-green"><div data-v-a7f43302="" class="flex items-center justify-center space-x-2">

<div data-v-a7f43302="" class="flex items-center justify-center space-x-2"><!---->

<span data-v-a7f43302=""> Create Disbursement </span>

</div>
</button>
</div>

 

[这是按钮的样子] :https://i.stack.imgur.com/cml4u.png

我的代码是这样的:

WebElement createDisburseButton = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"disbursement-root\"]/div/div[1]/div/button")));

createDisburseButton.click();

错误:

Expected condition failed: waiting for element to be clickable: By.xpath: //*[@id="disbursement-root"]/div/div[1]/div/button (tried for 10 second(s) with 500 milliseconds interval)

【问题讨论】:

  • 为了能够提供帮助,请提供失败的执行代码(java 类)、更扩展的 DOM 以及与问题相关的大量堆栈跟踪。根据目前提供的信息,问题可能是什么。还提供指向网页的链接,如果该链接是内部的,请提供您希望单击的内容的屏幕截图,同时通过 F12 选项显示该按钮与 DOM 的检查
  • 你遇到了什么错误?
  • 我编辑了描述,感谢@djmonki 的帮助
  • 我编辑了描述,感谢@cruisepandey 的帮助
  • @Clara:请看下面我的回答。

标签: python java selenium xpath automation


【解决方案1】:

要点击创建支付,您可以使用以下任一Locator Strategies

  • 使用 Javaxpath:

    driver.findElement(By.xpath("//span[contains(., 'Create Disbursement')]")).click();
    
  • 使用 Pythonxpath:

    driver.find_element(By.XPATH, "//span[contains(., 'Create Disbursement')]").click()
    

【讨论】:

  • 仍然无法使用 java 。我输入了Thread.sleep(10000); System.out.println("disburse homepage fully loaded"); driver.findElement(By.xpath("//span[contains(., 'Create Disbursement')]")).click();,仍然得到no such element: Unable to locate element: {"method":"xpath","selector":"//span[contains(., 'Create Disbursement')]"}
  • 标记你 ^ @DebanjanB
【解决方案2】:

如果我们在HTML DOM 中有唯一条目,请检查dev tools(谷歌浏览器)。

你应该检查的xpath:

//span[contains(text(),'Create Disbursement')]/ancestor::button

检查步骤:

Press F12 in Chrome -> 转到element 部分 -> 做一个CTRL + F -> 然后粘贴xpath 看看,如果你想要的element 用@ 得到高亮 987654329@匹配节点。

如果我们有 1/1 匹配节点,请确保:

  1. 此 div 不在 iframe 下。
  2. 此 div 不在 shadow-root 下。
  3. 您不应该在 selenium 启动的新选项卡/窗口上。

如果您确定我们有 1/1 匹配节点,并且以上 3 个条件不满足。

那么你就可以使用下面的代码试用了。

在 Selenium 中基本上有 4 种点击方式。

我将使用这个 xpath

//span[contains(text(),'Create Disbursement')]/ancestor::button

代码试用 1:

time.sleep(5)
driver.find_element_by_xpath("//span[contains(text(),'Create Disbursement')]/ancestor::button").click()

代码试用 2:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'Create Disbursement')]/ancestor::button"))).click()

代码试用 3:

time.sleep(5)
button = driver.find_element_by_xpath("//span[contains(text(),'Create Disbursement')]/ancestor::button")
driver.execute_script("arguments[0].click();", button)

代码试用 4:

time.sleep(5)
button = driver.find_element_by_xpath("//span[contains(text(),'Create Disbursement')]/ancestor::button")
ActionChains(driver).move_to_element(button).click().perform()

进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

【讨论】:

  • 我已经尝试过你所有的代码试用,但没有一个工作抱歉:(我问我的开发人员他说我需要查看 HTML 的日志才能访问孩子。我不知道他的意思是什么,但我想我需要搜索如何做到这一点@cruisepandey
  • 但我很感谢你的所有回答,我感激不尽
  • 第一次试代码的错误是什么?
猜你喜欢
  • 2021-08-19
  • 1970-01-01
  • 2021-01-03
  • 2021-05-11
  • 2017-11-30
  • 1970-01-01
  • 2014-01-12
  • 2013-09-03
  • 2019-08-03
相关资源
最近更新 更多