Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Python + selenium Pit jumping Tour (first bullet)

Shulou Source: shulou.com Published: 2022-06-03 05:16:43 11月13日 Update

No matter how proficient you are in selenium, the most important thing that accompanies testers is positioning.

The first pit:

For the page to be located, there is html nesting, that is, ifrmae. You can find this intuitively on the html code page. In fact, a sub-page is referenced in a page. As a result, we can't find the element directly when we use xpath or css to locate. At this time, we need to use a new thing to locate her, the general idea is: the first step is to switch from the parent page to the child page; the second step is to locate the elements in the child page; and the third step is to return to the parent page (this depends on personal needs. Specific where to stay needs to be combined with reality).

The implementation of the first step requires one method: switch_to_frmae. For specific usage, please refer to the code. Here, I must remind you that switch_to_frmae may not work because of version problems, which may need to be changed to switch_to.frmae. One'_', one'. I've been climbing this hole for a long time.

The second step is relatively simple. We can copy the xpath or css path with firebug. It is worth noting that this time the path starts from the child page, has nothing to do with the parent page, if you want to use id,class and so on, it should be possible (of course, I have not tried, I am not responsible for this sentence).

The third step, return, we need to exit when we don't need to locate in this sub-page, which requires a new method.

Switch_to.default_content (). The specific content can be Baidu or refer to my code map. At this point, the first pit jumped out. When positioning elements, be sure to pay attention to element or elements, a singular, a plural, the meaning is really too big difference. When you find_element_by_id ('p') is just an id ='p' element, find_elements_by_id ('p') is actually a list of id ='p'. Remember that you want one or more elements, and remember. The code I posted was a problem when I tested the account password to log in to QQ Mail. This time, the problem is not the code, but it may be due to the network or some other reason. When I located QQ Mail's' login 'button, click () kept showing the word "server busy". At first, I thought it was clicked too fast, and this was still the case after I set time.sleep (3) (invisible waiting I didn't try. But interested students can try), in order to solve this problem, I have no choice but to click (), then time.sleep (3), and then click (), unexpectedly miraculously successful.

Tags: Page location time problem code element or thing size actual method path mailbox test login busy successful intuitive personal that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Apple OPPO Reno Linux vpn