我使用Python
和Selenium
来自动化测试用例。问题是当我到达test_press_add_to_cart
时Webdriver
没有看到xpath
的元素:
//*[@id="fybAddCartEvent"]
可能是什么问题?
例外是:
" TimeoutException:Message"
元素的HTML
是:
<a href="#" class="button" data-product_id="18542" data-wp_nonce="7c3d595f98" id="fybAddCartEvent">
Add to cart</a>
脚本:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.webdriver.common.action_chains import ActionChains
from HTMLTestRunner import HTMLTestRunner
class Fotball_add_to_cart(unittest.TestCase):
@classmethod
def setUpClass(inst):
inst.driver = webdriver.Chrome('C:/chromedriver/chromedriver.exe')
driver = inst.driver
driver.get("http://ak:akpass@uat.athleticknit.com/football/")
inst.driver.maximize_window()
time.sleep(5)
#click on "View All Fotball Products"
def test_click_on_view_all_fotball_products(self):
viewProductsXpath = "a.woocommerce-nested-category-layout-see-more"
self.viewProductsElement = self.driver.find_element_by_css_selector(viewProductsXpath)
self.viewProductsElement.click()
time.sleep(7)
#select a product
def test_select_a_product_and_view_details(self):
#select product
tshirtXpath = "//a[@href=\"http://uat.athleticknit.com/product/f810/F810-000/\"]"
self.tshirtElement = self.driver.find_element_by_xpath(tshirtXpath)
self.tshirtElement.click()
time.sleep(60)
def test_press_add_to_cart(self):
#press add to cart
driver = self.driver
addToCartXpath = '//*[@id="fybAddCartEvent"]'
self.addToCartElement =WebDriverWait(driver, 20).until(lambda driver: driver.find_element_by_xpath(addToCartXpath))
self.addToCartElement.click()
time.sleep(5)
@classmethod
def tearDownClass(inst):
inst.driver.stop_client()
inst.driver.close()
inst.driver.quit()
答案 0 :(得分:1)
实际上你在这里使用了Explicit wait
tables = ls(envir=.GlobalEnv)[sapply(ls(envir=.GlobalEnv),
for (i in 1:length(tables)){
df=read.table(tables[[i]], sep="\t", header=T); rownames(df) = df[,1]}
}
所以它等待你的情况直到20秒,如果条件不满意则显示tables = ls(envir=.GlobalEnv)[sapply(ls(envir=.GlobalEnv),
for(i in 1:length(tables)){
tables[[i]] <- tables[[i]][,-1]
rownames(tables[[i]]) <- tables[[i]][,1]}
}
。
如果您要求等待“添加到购物车”&#39;然后尝试以下方式 -
self.addToCartElement =WebDriverWait(driver, 20).until(lambda driver: driver.find_element_by_xpath(addToCartXpath))
注意: - 如果你有元素id,那么请按ID而不是xpath
找到元素TimeoutException