【问题标题】:xPath doesn't go on to the next pagexPath 没有进入下一页
【发布时间】:2019-05-22 11:06:19
【问题描述】:

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

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 不起作用。

【问题讨论】:

    标签: python testing firefox selenium-webdriver xpath


    【解决方案1】:

    您好,祝您学习自动化测试好运。

    当 xpath 不起作用时,我做的第一件事就是检查它,通常使用像 this 这样的扩展名来确保它是正确的。此外,最好使用更短的 xpath,这样出错的空间就会更小,即"//img[@title='Printed Dress']"

    【讨论】:

      【解决方案2】:

      尝试以下xpath并使用javaScript executor点击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)  
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-10
        • 1970-01-01
        • 2016-07-28
        • 2018-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多