Python使用handless时需要校验操作系统!!!

版权声明:本文为博主原创文章,转载请声明出处。 https://blog.csdn.net/qq_28311921/article/details/86569583
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from config import EXECUTABLE_PATH, CHROME_PATH
from sys import platform

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_argument('--allow-running-insecure-content')
chrome_options.add_argument('disable-infobars')
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('no-sandbox')
caps = DesiredCapabilities.CHROME
if not caps:
    raise Exception('请安装并配置chrome handless...')
caps['loggingPrefs'] = {'performance': 'ALL'}
if platform == "linux" or platform == "linux2":
    if not EXECUTABLE_PATH:
        raise Exception('请安装并配置EXECUTABLE_PATH...')
    chrome_options.binary_location = CHROME_PATH
    driver = webdriver.Chrome(EXECUTABLE_PATH, desired_capabilities=caps, chrome_options=chrome_options)
elif platform == "darwin":
    driver = webdriver.Chrome(desired_capabilities=caps)
else:
    raise Exception('暂不支持os x及linux以外的其他操作系统...')


driver.get('https://wx.qq.com/?&lang=zh_CN')
time.sleep(10)
driver.find_element_by_xpath('//h3/span[contains(text(), "文件传输助手")]').click()
content = driver.find_element_by_xpath('//div[@class="content ng-isolate-scope"]/pre[@id="editArea"]')
ActionChains(driver).move_to_element(content).perform()
pub_url = 'https://search.weixin.qq.com/cgi-bin/searchweb/clientjump?tag=wxindex'
index_url = 'https://search.weixin.qq.com/cgi-bin/h5/wxindex/detail.html?q=%e5%8c%ba%e5%9d%97%e9%93%be#wechat_redirect'
driver.find_element_by_xpath('//div[@class="content ng-isolate-scope"]/pre[@id="editArea"]').send_keys(pub_url)
driver.find_element_by_xpath('//div[@class="content ng-isolate-scope"]/pre[@id="editArea"]').send_keys(Keys.ENTER)
driver.find_element_by_xpath('//div[@class="content ng-isolate-scope"]/pre[@id="editArea"]').send_keys(index_url)
driver.find_element_by_xpath('//div[@class="content ng-isolate-scope"]/pre[@id="editArea"]').send_keys(Keys.ENTER)
driver.find_element_by_xpath('//div[@class="ng-scope"]/div[@class="clearfix"]//div[@class="plain"]').click()
windows = driver.window_handles
driver.switch_to.window(windows[1])
time.sleep(2)
print(driver.current_window_handle)
driver.close()
driver.switch_to.window(windows[0])
print(driver.current_window_handle)
time.sleep(3)
driver.find_element_by_xpath('//div[@class="ng-scope"]/div[@class="clearfix"]//div[@class="plain"]')
time.sleep(3)
print(windows)

使用selenium时,根据开发和生产环境的不同使用的配置也是不一样的,一部分配置放在环境变量内,一部分需要放在代码内。当然,如果可以不建议使用selenium。

猜你喜欢

转载自blog.csdn.net/qq_28311921/article/details/86569583
今日推荐