[英]Unable to locate element using ID and xpath
我在定位按鈕時遇到問題。
https://buggy-testingcup.pgs-soft.com/task_1
我想通過使用按鈕“Dodaj”將 101 件商品添加到購物籃中,以檢查是否出現警報。
<div class="input-group input-group-sm">
<span class="input-group-btn">
<button id="add-product-5e9847b5ee071" class="btn btn-sm" role="button" data-add-to-basket="" data-product-price="15.54" data-product-name="Okulary">Dodaj</button>
</span>
<input type="number" min="0" step="1" class="form-control" value="0" autocomplete="off">
</div>
我嘗試使用 xpath
add = self.driver.find_element_by_xpath('//*[@id="add-product-5e9847b5ee071"]')
還有 ID“add-product-5e9847b5ee071”,但我收到消息:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="add-product-5e9847b5ee071"]"}
有人可以解釋我有什么問題嗎?
請注意,雖然每個產品 Dodaj 按鈕都有一個可以更改的唯一 id,但它們都具有與容器對應的相同屬性 data-product-name。 例如,對於 Piłka,如果您想要 Dodaj 按鈕,您可以使用以下
elem = driver.find_elements_by_xpath('//button[@data-product-name="Piłka"]')
輸入數據后點擊第一個Dodaj
按鈕
WebDriverWait
() 並等待element_to_be_clickable
() 並跟隨 xpath。
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 import webdriver
driver=webdriver.Chrome()
driver.get("https://buggy-testingcup.pgs-soft.com/task_1")
inputtext=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"(//input[@class='form-control'])[1]")))
inputtext.clear()
inputtext.send_keys("101")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"(//button[text()='Dodaj'])[1]"))).click()
多了一個xpath選項。
driver=webdriver.Chrome()
driver.get("https://buggy-testingcup.pgs-soft.com/task_1")
inputtext=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//h4[text()='Okulary']/following::input[1]")))
inputtext.clear()
inputtext.send_keys("101")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//h4[text()='Okulary']/following::button[1]"))).click()
瀏覽器快照:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.