[英]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.