[英]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
:使用Java和
xpath
:
driver.findElement(By.xpath("//span[contains(., 'Create Disbursement')]")).click();
Using Python and xpath
:使用Python和
xpath
:
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 匹配节点,请确保:
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.