程序找不到下一个按钮

问题描述

我正在编写一个单元测试程序来测试我站点链接,如果一切正常,则返回确定。我一直遇到的一个问题是,它的运行不是很一致,它会找到所有特定的元素,而下一个则找不到。我觉得这可能是由于我的互联网连接所致,因为当我连接到手机热点时,它运行起来更加流畅。但是,如果该程序与它的结果不一致,我就不能依靠该程序进行网站测试。现在,除了找到此元素next_arrow之外,一切都运行顺利。我使用xpath作为按钮,但它继续说无法找到。我还在单独的程序中使用了xpath,只是为了真正查看它是否不起作用,并且程序2找到了该元素并反复单击它。也许我已经看了太久了,对您的帮助还是很大的。

def test_e_transparency_page(self):
        self.posts = ['//*[@id="pgi86feccece3844ba09f80aa6696eb037f_0"]','//*[@id="pgibb0246bf32f6468189670ff4c02f48d6_1"]','//*[@id="pgi561b043aafbf428aa532ca1212803c15_2"]']
        self.compare = ["//span[contains(text(),'what you should kNow about your CBD oil')]","//span[contains(text(),'Essential Oil Facts')]",'the benefits of natural butters & oils')]"]
        self.intext = ["what you should kNow about your CBD oil","Essential Oil Facts","the benefits of natural butters & oils","Hemp Facts Topics"]
        self.exits = ["//*[@id='comp-jvfpiv1wsvgcontent']","//*[@id='comp-jvfii91usvgcontent']","//*[@id='comp-jvfpdcbwsvgcontent']"]
        self.errormsgs = ["Our Farms Error","Essiental Oil Benifits","Butters and Oils"]
        self.tran.click()
        time.sleep(3)
        assert "Transparency | Black Hemp Family" in self.driver.title,"Transparency Button Link Error"
        time.sleep(5)
        next_arrow = self.driver.find_element_by_tag_name('button')
        for (post,c,text,exit_button,error) in zip(self.posts,self.compare,self.intext,self.exits,self.errormsgs):
            self.driver.find_element_by_xpath(post).click()
            time.sleep(3)
            title = self.driver.find_element_by_xpath(c)
            time.sleep(1)
            assert text in title.text,error
            self.driver.find_element_by_xpath(exit_button).click()
            time.sleep(3)
            next_arrow.click()

        cbd_info = self.driver.find_element_by_xpath("//div[@id='pgi6faab6bad5984f57a41cfd82c3623871_3']").click()
        time.sleep(3)
        assert self.intext[3] in self.driver.title,"CBD Info Link Error"

 

解决方法

可以为您的循环尝试吗?

for (post,c,text,exit_button,error) in zip(self.posts,self.compare,self.intext,self.exits,self.errormsgs):
self.driver.find_element_by_xpath(post).click()
time.sleep(3)
title = self.driver.find_element_by_xpath(c)
time.sleep(1)
assert text in title.text,error
self.driver.find_element_by_xpath(exit_button).click()
time.sleep(3)
next_arrow = self.driver.find_element_by_tag_name('button')
if next_arrow:
    next_arrow.click()