Linux hangup python program details
Solenium is a complete automated testing system for web applications.
Installation steps:
- Install Selenium: pip3 install selenium
- Install webdriver:
Chrome: http://chromedriver.storage.googleapis.com/index.html
Chromium: sudo apt-get install chromium-chromedriver
Firefox: https://github.com/mozilla/geckodriver/releases/
Note: Webdriver needs to match the corresponding browser version with the serenium version. - Webdriver installation path
Win: Copy webdriver to python installation directory
Linux/Mac: Copy webdriver to/usr/local/bin
Selenium Basic Operations and Element Positioning
Start Browser
Start the crhome browser
code
| 1 2 3 4 | from selenium import webdriver driver = webdriver.Chrome() driver.get(‘http://xxx.com’) |
Start Chromium Browser
code
| 1 2 3 4 5 | from selenium import webdriver #apt-get install webdriver path driver = webdriver.Chrome(‘/usr/lib/chromium-browser/chromedriver’) driver.get(‘http://xxx.com’) |
Specified drive path startup
code
| 1 2 3 4 5 6 7 | from selenium import webdriver # Drive path driver_path = r’D:\download\chromedriver.exe’ driver = webdriver.Chrome(executable_path=driver_path) driver.get(‘http://xxx.com’) |
Headless is activated.
Headless Chrome is the uninterfaced form of the Chrome Browser, which runs your program without opening the browser and using all Chorome-supported features.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | from selenium import webdriver # Do not show graphical interfaces option = webdriver.ChromeOptions() # Add no interface options option.add_argument(“–headless”) # If you don’t add this option, there are times when location is a problem option.add_argument(“–disable-gpu”) # Let Chrome run under root option.add_argument(‘–no-sandbox’) option.add_argument(‘–disable-dev-shm-usage’) driver = webdriver.Chrome(chrome_options=option) driver.get(‘http://xxx.com’) |
Element Positioning
id positioning: find_election_by_id()
code
| 1 2 | # Position through id and enter the value selenium driver.find_element_by_id(‘kw’).send_keys(‘selenium’) |
Name location: find_election_by_name()
code
| 1 2 | # Position by name and enter value selenium driver.find_element_by_name(‘wd’).send_keys(‘selenium’) |
calass location: find_election_by_class_name()
code
| 1 2 | # Positioning through class and input value selenium driver.find_element_by_class(‘wd’).send_keys(‘selenium’) |
tag location: find_election_by_tag_name()
HTML defines functions by tag, e.g. input is input and table is table.
code
| 1 2 | # Positioning through the tag and entering a value of serenium driver.find_element_by_tag(‘input’).send_keys(‘selenium’) |
link location: find_election_by_link_text()
This method is specifically designed to locate text links, such as “news”, “maps” and so on at the top right corner of the 100-degree front page.
code
| 1 2 | # Locating the link “news” and clicking on .find_election_by_link_text (‘news’).click() |
Partial_link Positioning: find_election_by_Partial_link_text()
Sometimes a hyperlink text is long, and if we enter all of it in trouble and look bad code, then we can just cut out one part of the string, and this way it’s vaguely matched.
code
| 1 2 | # To locate the link “News” and click on it through partial_link driver.find_element_by_partial_link_text(‘闻’).click() |
xpat location: find_election_by_xpath()
code
| 1 2 | # locate the search box through xpath and enter serenium driver.find_element_by_xpath(“//*[@id=’kw’]”).send_keys(‘selenium’) |
CSS location: find_election_by_cs_selector()
code
| 1 2 | # Locating through the CSS search box and entering serenium driver.find_element_by_css_selector(‘#kw’).send_keys(‘selenium’) |
jQuery Positioning
The basic syntax is (selector).action(), action() symbol definition of jQuery, sector selector to query specific HTML elements and to perform specific operation on the element through action().
Of these, the accelerator() that we often use is one of several in jq:
- $(sector).val (`input_value ‘) input_value indicates the value of the text to be entered
- $(selector).val(‘) if empty, execution means emptiness.
- $(selector).click()
code
| 1 2 3 4 | search_jq = “$(‘#kw’).val(‘selenium’)” button_jq = “$(‘.s_btn’).click()” driver.execute_script(search_jq) driver.execute_script(button_jq) |
Selenium’s three forms of waiting.
Sometimes, in order to ensure the stability of script operation, waiting time is added to the script.
Force waiting
Forced waiting: Whether the browser is loaded or not, the program will have to wait three seconds, three seconds before it reaches the code below.
code
| 1 2 3 4 5 6 7 | from selenium import webdriver import time driver = webdriver.Chrome() driver.get(‘http://xxx.com’) time.sleep(3) |
Invisible waiting.
Invisible waiting: by adding an impllicitly_wait() method, you can be intelligent in a time frame.
Invisible waiting is the setting of a maximum waiting period, and if the loading of the web page is completed within the specified time frame, the next step will be executed or until the deadline is reached before the next step is implemented.
Implicit waiting is working for the whole driver cycle, so just set it up once.
code
| 1 2 3 4 5 6 7 | from selenium import webdriver driver = webdriver.Chrome() # 30s maximum wait driver.implicitly_wait(30) driver.get(‘http://xxx.com’) |
Obvious waiting.
Remarkable waiting: WebDriverWater, in combination with this type of unil() and unil_not() method, is flexible enough to wait on the basis of judgement. Its main meaning is that the program looks every n-second, if conditions are established, executes the next step or continues until it exceeds the maximum set period and throws out TimeoutException.
Browser Operations
Browser Maximise & Minimize
code
| 1 2 | driver.maximize_window() driver.minimize_window() |
Set Window Size
code
| 1 2 | # 480 wide, 800 high driver.set_window_size(480,800) |
Move back!
code
| 1 2 | driver.forword() driver.back() |
Browser Tab Jump
code
| 1 2 3 4 | from selenium.webdriver.common.action_chains import ActionChains # When you open a new page driver.switch_to.window(driver.window_handles[1]) |
Operation Test Object
The following methods are common in webdriver:
- click: Click objects
- Send_keys: Simulate key input on object
- Clear: clear the contents of objects, if possible
- Submit: Content of the subject(s) to be submitted, if available
- text information for the acquisition of elements
Mouse Time
Mouse events typically include right mouse buttons, double clicks, drags, moves to an element, etc.
code
| 1 2 3 4 5 6 7 8 9 10 11 | from selenium.webdriver.common.action_chains import ActionChains # ActionChains common method: #perform() perform all actionChains stored actions #Context_click() Right # double_click() double-click # Drag_and_drop() drag # move_to_election() mouse over # Move the mouse to an absolute position on the page and click ActionChains(driver).move_by_offset(100, 230).click().perform() |
Multi-layer framework, tier positioning
The problem of missing elements is often encountered in the process of locating them, which generally results from:
- The element is not positioned correctly.
- The page contains a frame or embedded window
- Page Timeout
Webdriver offers a switch_to_frame method that can easily solve this problem.
| 1 | driver.switch_to_frame(“f1”) |
Close Browser
code
| 1 | driver.quit() |
Run the web automation program using shell scripts
code
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #! /bin/bash # Enter the path of the script file cd /home/pi/autoWeb # Recording call times echo $(date “+%Y-%m-%d %H:%M:%S”) >> /home/pi/autoWeb/log.txt python3 autoWeb_c.py echo “Complete the form – cui” >> /home/pi/autoWeb/log.txt # Shell uses the symbol “$?” to show the return value of the previous command, and if 0 is a successful execution, other means a failure if [$? -ne 0]; then echo “fail: cui” >> /home/pi/autoWeb/log.txt else python3 autoWeb_l.py echo “Complete the form – liu” >> /home/pi/autoWeb/log.txt F echo “” >> /home/pi/autoWeb/log.txt |
Linux performs a time-bound mission: Cron
The cron process inside of the linux will help us achieve a time-bound mission.
| 1 2 3 | Crontab-e (editing sheets) Crontab-l (lists the commands in the sheet) Crontab-r (deleted Tasks done) |
Crontab-e enters the current user ‘ s sheet editing, which is a common vim interface. Each line is a command.
Crontab’s orders are time + actions, and they have minutes, hours, days, months, Fridays.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | # Example 1: MyCommand is executed every 1 minute * * * * * * myCommand # Example 2: 3rd and 15th minute per hour 3.15 * * * * * myCommand # Example 3: at the 3rd and 15th minutes from 8 to 11 a.m. 3.15 8-11 * * * myCommand # Example 4: Every two days between 8 and 11 a.m., 3rd and 15th minutes 3.15 8-11 */2 * * myCommand # Example 5: implemented Monday between 8 and 11 a.m., 3rd and 15th minutes per week 3.15 8-11 * * 1 myCommand # Example 6: 21:30 a night to restart smb 30 21 * * * /etc/init.d/smb restart |
Sets up an automated web script at 12 am every day.
code
| 1 | 10 00 * * * /home/pi/autoWeb/autoWeb.sh |
Restart curon service
code
| 1 | service cron restart |
未经允许不得转载:萌萌Apprentissage » Selenium+Python Environment Configuration
linux One Chinese script
Google shell enable enhanced mode 4 nuclear 16G RAM
Python batches to extract mainstream search engine pictures
Python's word crawling across 100 degrees.
PythonTaiichi Taipei Artificial Intelligence Tensoflow Graphic Processing
Selenium with Tor Browser using Python