In the process of automated testing, we often encounter situations where the control cannot be located. The specific error messages thrown are as follows:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"email"}
Copy code
But let’s take a look at the page code and then at the code written by ourselves
emailName = driver.find_element_by_name('email')
Copy code
That's right, this can't be wrong, it should definitely be able to locate, then there are usually several reasons, the first is the lack of waiting time, the second is multi-window, the third is definitely iframe.
If we look for the Shun Teng module, we will find that, oh, there is indeed an iframe. . .
So how to deal with iframe, if our iframe has id or name, and the attribute value of id or name is not empty, and the attribute value of id and name is not dynamically changed,
we can directly cut into it as follows In iframe:
driver.switch_to.frame('x-URS-iframe')
Copy code
If id or name is dynamic, what is dynamic? For example, the id attribute value like the following
id="auto-id-1604073488440"
Copy code
How to do this? It doesn’t matter, just use a positioning method to locate the iframe first, and then cut in
dd = driver.find_element_by_xpath("//div[@id='loginDiv']/iframe")
driver.switch_to.frame(dd)
emailName = driver.find_element_by_name('email')
Copy code
At this point you are done. .
Of course, what if you want to cut out from the iframe?
driver.switch_to.parent_frame([Edit, Test and Edit] The first software test challenge is waiting for you to sign up to win the prize!)#Switch from the child frame to the parent frame
driver.switch_to.default_content()#Switch back to the main document