我嘗試使用 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)
uj5u.com熱心網友回復:
要單擊,Create Disbursement您可以使用以下任一定位器策略:
使用Java和
xpath:driver.findElement(By.xpath("//span[contains(., 'Create Disbursement')]")).click();使用Python和
xpath:driver.find_element(By.XPATH, "//span[contains(., 'Create Disbursement')]").click()
uj5u.com熱心網友回復:
請檢查dev tools(谷歌瀏覽器)我們是否有唯一的條目HTML DOM。
您應該檢查的 xpath :
//span[contains(text(),'Create Disbursement')]/ancestor::button
檢查步驟:
Press F12 in Chrome- >去element節- >做一個CTRL F- >再貼上xpath看看,如果你需要的element是越來越強調與1/1匹配的節點。
如果我們有 1/1 匹配節點,請確保:
- 這個 div 不在 iframe 之下。
- 此 div 不在 shadow-root 下。
- 您不應該在 selenium 啟動的新選項卡/視窗上。
如果你確定我們有 1/1 匹配的節點,并且以上 3 個條件不滿足。
然后你可以使用下面的代碼試用。
基本上有 4 種方法可以在 Selenium 中單擊。
我將使用這個 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/362318.html
