简体   繁体   中英

Unable to locate element using ID and xpath

I have a problem with locating button.

https://buggy-testingcup.pgs-soft.com/task_1

I want to add 101 items to basket by using button "Dodaj" to check if alert appears.

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

I tried to use xpath

add = self.driver.find_element_by_xpath('//*[@id="add-product-5e9847b5ee071"]')

and also ID "add-product-5e9847b5ee071" but I get message:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="add-product-5e9847b5ee071"]"}

Can someone explain me what is wrong?

Note that while each product Dodaj button has a unique id that changes, they all have the same attribute data-product-name which corresponds to the container. For example, for Piłka, if you want the Dodaj button, you can use the following

elem = driver.find_elements_by_xpath('//button[@data-product-name="Piłka"]')

To click on the first Dodaj button after entering data induce

WebDriverWait () and wait for element_to_be_clickable () and following 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()

One more xpath option.

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

Browser snapshot:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM