简体   繁体   English

找不到 By.xpath 定位的元素

[英]Can't find element located by By.xpath

i tried to click this button with xpath, classname, and css locator.我尝试使用 xpath、类名和 css 定位器单击此按钮。 I even put a time wait so it can load properly first, but nothing worked.我什至等待了一段时间,以便它可以先正确加载,但没有任何效果。 still can't click the button, please help.仍然无法点击按钮,请帮助。

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

 

[this is the button looks like]: https://i.stack.imgur.com/cml4u.png [这是按钮的样子]: https://i.stack.imgur.com/cml4u.png

my code was like this one:我的代码是这样的:

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

createDisburseButton.click();

the error:错误:

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)

To click on Create Disbursement you can use either of the following Locator Strategies :要单击创建付款,您可以使用以下任一定位器策略

  • Using Java and xpath :使用Javaxpath

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

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

Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.如果我们在HTML DOM中有唯一条目,请检查dev tools (谷歌浏览器)。

xpath that you should check:您应该检查的 xpath:

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

Steps to check:检查步骤:

Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node. Press F12 in Chrome -> go 到element部分 -> 执行CTRL + F -> 然后粘贴xpath并查看,如果您想要的element使用1/1匹配节点突出显示

If we have 1/1 matching node, Please make sure that:如果我们有 1/1 匹配节点,请确保:

  1. This div is not under an iframe.此 div 不在 iframe 下。
  2. This div is not under a shadow-root.此 div 不在 shadow-root 下。
  3. You should not be on new tab/windows launched by selenium.您不应该在 selenium 启动的新选项卡/窗口上。

If you are sure that we have 1/1 matching node, and above 3 conditions did not meet.如果您确定我们有 1/1 匹配节点,并且以上 3 个条件不满足。

Then you can use the below code trial.然后你可以使用下面的代码试用。

There are basically 4 ways to click in Selenium. Selenium中基本上有4种点击方式。

I will use this xpath我将使用这个 xpath

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

Code trial 1:代码试用一:

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

Code trial 2:代码试用2:

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

Code trial 3:代码试用3:

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

Code trial 4:代码试用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()

Imports:进口:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用 by.XPATH 找到元素 - Can't find element using by.XPATH 无法使用 By.XPATH 包含(文本() - Can't locate element using By.XPATH contains(text() Selenium find(By.XPATH) 无法使用 xpaths 数组定位元素 - Selenium find(By.XPATH) cannot locate element using array of xpaths find_element(By.XPATH) 不工作 python selenium - find_element(By.XPATH) not working python selenium Python selenium present_of_element_located((By.XPATH, ) 需要不区分大小写 - Python selenium presence_of_element_located((By.XPATH, ) need case insensitive 如何找到通过链接文本定位的元素的xpath? - How can I find the xpath of an element located by link text? “ find_element_by_xpath”和“ driver.find_elements(By.XPATH)”的区别是什么? - What is the difference in “find_element_by_xpath” and “driver.find_elements(By.XPATH)” 尝试使用 find_element(By.XPATH) 并在 FULL Xpaths 列表中获取元素不可交互错误 - Trying to use find_element(By.XPATH) and getting element not interactable error on list of FULL Xpaths 是否有一个 Selenium 包装器来查找具有给定选择器类型作为输入的元素(By.css_selector、By.xpath、By.class ...)? - Is there a Selenium wrapper to find element with a given selector type as an input (By.css_selector, By.xpath, By.class ...)? 用xpath找不到元素Selenium python - Can't find element selenium python with xpath
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM