当前位置:网站首页>For the first time, I suspect that there is a bug in selenium4 because the iframe element is not found?

For the first time, I suspect that there is a bug in selenium4 because the iframe element is not found?

2022-08-11 08:12:00 I tested first

前因

Since the beginning of selling skills in Xianyu,More than a dozen or 20 visitors have come to pay for answers,I am not very honored either、受宠若惊,
But one thing is not so beautiful,Great value for money for those looking for answers,But it is a loss for the subject,甚至是亏本;
不论如何,All in all, it's kind of worth it,can be found by those in need,To prove that their skills are valuable and needed,很不错.

后果

What I am best at ispython/java自动化测试领域,Naturally, it is in the line of automation,But it has also received a lot of demand from other fields;
譬如:matplotlib、flask、django、numpy、nltk等等方面,Of course, some learning costs are low,Can be dealt with in an emergency;
Sometimes aboutwebDevelopment is a bit stretched,every time you assess your needs,must build the environment,And this cost is not billed.

正题

It's hard to come up with a list of solutions to automated testing problems,不管是接口还是UI自动化,I don't have the above troubles;Because the development environment is ready-made,But the tested environment is varied.
  • 需求
UI自动化:模拟登录163邮箱,Why does the browser open every time,But can't enteraccount/passwd?
  • 分析1
Now that it has come to the point of opening the browser,Then you cannot enter the account operation?It is nothing more than that the element cannot be found,When I saw her code,我整个人都不淡定了.
  • 代码1:测试用例类
import unittest
from login import PageLogin

class TestLogin(PageLogin):
    
    def testcaselogin(self):
        username = "XXXXX"
        PWD  = "XXXXXX"

    def page_login(self,username,password):
        username = "XXXXXX"
        PWD  = "xxxxxXX"
        self.page_input_username(username)
        self.page_input_password(PWD)
        self.page_click_login_btn()
  • 代码2:The encapsulated page operation class
from base.basepage import Base

class PageLogin(Base):

    #登录
    def page_click_login_link(self):
        self.base_click(page.login_link)

    #输入用户名
    def page_input_username(self,email):
        self.base_input(page.login_username,email)
        login_username = By.name,"email"

    #输入密码
    def page_input_password(self,password):
        self.base_input(page.login_password,password)
        login_pwd = By.name,"password"

    #点击登录
    def page_click_login_btn(self):
        self.base_click(page.login_btn)
        login_btn = By.id,"dologin"
  • 分析2
From the above code level,A test case is executed(I saw it remotely to find out what she was usingvc编辑器),Of course you can only open the browser,
因为它是继承了PageLogin类,There is an open address operation,In the current test class, the open operation is not performed,But it didn't perform the account operation for the second reason;
原因就是:page.login_password,This element is also annotated,The entire test case simply cannot find the element to operate on;
  • PO设计模式实现的UI自动化测试框架,It feels like her code is cobbled together,And still don't fully understand this design pattern.

解决

In line with the mission of collecting people's money and people's disaster relief,解决这类问题,Ask yourself or you can do it!
  • Facts have proven themselves wrong,Though the idea is fine,But the direction may be deviated.
pageI filled her with the elements,Fortunately, these page elements are simple,都是有id或者name的.
Just because she used itselenium4的版本,This made me suspect at one point that it was a problem,The subject has also been unable to locate the element?
  • 思考:都是id或者nameThere is no reason why the element cannot be located,There must have been an oversight.
I also thought about itframe或iframe标签,But for the first timeF12's source code search yielded no results?
  • Makes me suspiciousselenium4,Use it immediately when you get homeselenium3版本试了一下,大失所望!Finally confirmed itiframe的问题,Open a browser and enter the destination address,It is not specifiediframeManipulate elements on this tag,Direct manipulation is invalid,As a result, the code keeps reporting an error that the page element cannot be found and times out.

代码

From asking a question to confirming it、找到问题、解决问题,The time cost is quite high,Because only received9.9元.
  • The modified test class
import unittest

from page.login import PageLogin


class TestLogin(unittest.TestCase):
    
    
    @classmethod
    def setUpClass(cls):
        cls.PL = PageLogin()
    
    
    def test_page_login(self):
        username = "XXXXXX"
        PWD  = "XXXXXXXX"
        self.PL.switch_iframe()
        self.PL.page_input_username(username)
        self.PL.page_input_password(PWD  )
        self.PL.page_click_login_btn()
        
if __name__ == '__main__':
    unittest.main()
  • Add methods to the base class
def switch_iframe(self):
    self.driver.switch_to.frame(self.driver.find_elements(By.TAG_NAME, "iframe")[0])
    # self.driver.switch_to.frame(self.base_find_element((By.XPATH,"//iframe[starts-with(@id,'x-URS-iframe')]")))
    # self.driver.switch_to.frame(0)
    # self.driver.switch_to.frame("id or name")

总结

So don't be arrogant,戒骄戒躁,不要粗心,It was really a problem that could be solved in a few minutes,to waste1整天.
对于UIThe problem with automation is nothing more than solving the problem of positioning page elements,As for what to think:How to find this element,How to write the best lookingxpath表达式等等,This requires practice.
原网站

版权声明
本文为[I tested first]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/223/202208110804042233.html