xPath不会转到下一页

时间:2019-05-22 11:06:19

标签: python testing firefox selenium-webdriver xpath

当我使用以下代码时,该网站不会进入下一页。

import unittest
from selenium import webdriver
import time

class ProductPurchase(unittest.TestCase):
    """
    Purchase the product on the website http://automationpractice.com/index.php
    """
    # Preconditions
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.get("http://automationpractice.com/index.php")
        self.driver.maximize_window()
    def teardown(self):
        self.driver.quit()
    # Buying a product on the website 
    def test_wrong_agreement(self):
        driver = self.webdriver
        time.sleep(2)
    #Click on "Quick view"
        quickview_btn = driver.find_element_by_xpath("/html/body/div/div[2]/div/div[2]/div/div[1]/ul[1]/li[1]/div/div[1]/div/a[2]").click()


if __name__ == '__main__':
    unittest.main(verbosity=2)

它应该转到下一页,但xPath不起作用。

2 个答案:

答案 0 :(得分:0)

您好,祝您学习自动化。

当xpath不起作用时,我要做的第一件事就是检查它,通常使用this这样的扩展名来确保它正确。此外,最好使用较短的xpath,以减少错误的发生空间,例如FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();

答案 1 :(得分:0)

尝试以下xpath并使用javaScript执行器单击Quick View 这段代码将单击页面上的第一个元素。如果要依次单击所有Quick Viw按钮,则需要进一步编写逻辑。

driver.get("http://automationpractice.com/index.php")
driver.maximize_window()
ele_Quikview=driver.find_element_by_xpath('(//a[@class="quick-view"])[1]')
driver.execute_script("arguments[0].click();",ele_Quikview)