A few more showy operations of python selenium

 Hello, friends, long time no see, next I will share with you a few more coquettish selenium operations, are you ready? The doors were welded shut and the car drove off. Hahaha

1. Open two pages and perform switching operations

  When we are doing some page tests, what we need to do is to open a new page on the basis of the current page and do some operations at the same time, then switch to another page and continue to do operations. This condition is if and Only when operating in the current driver object, instead of two driver objects, let’s not talk nonsense, here is the code:

1  from selenium import webdriver
 2  import time
 3  ​4
 def open_windows1():
 5      driver.get( ' www.baidu.com ' )
 6      time.sleep(9 )
 7 ​8
 def open_windows2(): #Second new The window uses the JS method to open a new window, simulating a new tab pop-up window 9      new_page= window.open( ' www.jd.com ' )
 10     driver.execute_script(new_page)
 11      time.sleep(2 )
 12 ​13
   
   def switch_windows():
14     handles = driver.window_handles
15     print('print currrent opened windows:' + str(handles))
16     while 1:
17         driver.switch_to.window(handles[0])
18         time.sleep(5)
19         driver.switch_to.window(handles[1])
20         time.sleep(5)
21 if __name__ == "__main__":
22     chromeOptions = webdriver.ChromeOptions()
23     chromeOptions.add_experimental_option('useAutomationExtension', False)
24     driver = webdriver.Chrome(chrome_options=chromeOptions, desired_capabilities=chromeOptions.to_capabilities())
25     driver.maximize_window()
26     open_widows1()
27     open_wondows2()
28     switch_windows()
click me click me

  Friends can directly copy this code and run it to see the effect. Of course, you can also use other methods to obtain this driver object. Isn’t it a bit embarrassing?

2. Manipulate invisible elements

    Some elements set the attribute display=none on the page. At this time, if you use ordinary xpath, you will definitely not be able to get it. At this time, you need to perform JS operations on this page, that is, to change the visibility of this element through JS, that is, through JS To change the attribute of this element, and then operate to obtain the value.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<select name="new_select" id="" style="display:none">
    <option value="bmw">BMW</option>
    <option value="audi">AUDI</option>
    <option value="mini">MINI</option>
    <option value="benz">BENZ</option>
</select>
</body>
</html>
sample html code

  Normally we want to get the value in the select option box, but we can't get it.

 1 from selenium import webdriver
 2 from selenium.webdriver.support.select import Select
 3 import os
 4 import time
 5 chromeOptions = webdriver.ChromeOptions()
 6 chromeOptions.add_experimental_option('useAutomationExtension', False)
 7 driver = webdriver.Chrome(chrome_options=chromeOptions, desired_capabilities=chromeOptions.to_capabilities())
 8 driver.maximize_window()
 9 file_path = 'file:///'+ os.path.abspath( ' test_display.html ' )
 10  driver.get(file_path)
 11 change_display = ' document.querySelectorAll("select")[0].style.display="block"; ' 
12  #This statement Just change the properties of the select option box, and then you can operate normally 
13  driver.execute_script(change_display)
 14 sel = driver.find_element_by_tag_name( ' select ' )
 15 Select(sel).select_by_value( ' mini ' )
 16 time .sleep(2 )
 17 driver.quit()
Click me to view Sao operation

3. The pop-up window element cannot be located

  In the selenium test, it seems to be a simple pop-up window, but it cannot be found by using alert and ordinary positioning. After checking again, it is not an iframe element (we say iframe element below), and then using sleep time also cannot Solution, one of our solutions at this time is to print all the windows of the current page, that is, to see how many windows there are in this page.

1  #That is to call the window_handles method to view the current total number of windows 
2 all_handles = driver.window_handles
 3  print (all_handles)
 4  #The result at this time is that this list has two elements, which means that the current page has two windows, so now What we see is one window, but actually there are two windows. The displayed element is in another window. We only need to switch to another window for processing, and then switch back to the original window after processing. 
5  #Switch method 
6 driver.switch_to.window(all_handles[1 ])
 7  #Switch back to the original window 
8 driver.switch_to.window(all_handles[0])

This can be tried using this method when we can’t find it using alert and other methods, maybe we can go down

4. Switch to the iframe element

    We can clearly see some pages, get the current element, and get the id of the current element, but we can’t locate it alive or dead. At this time, the element we need to locate is likely to be included in the iframe element. It is that we need to enter the iframe element so that we can operate.

#For example, the current iframe element id is 'frame' 
driver.switch_to.frame(driver.find_element_by_id( ' iframe ' )
 #Then you can perform normal operations, 
#Exit the current iframe 
driver.switch_to.default_content()
 #So we can Exit the iframe element and perform normal operations.

  More show operations are being explored, so stay tuned. . . .

Guess you like

Origin blog.csdn.net/bruce_van/article/details/104890919