python selenium实现自动操作chrome的某网站数据清洗【此篇为jupyter notebook直接导出.md】

背景

最近实习的时候进行数据清洗,需要非常繁琐却重复的操作chrome浏览器中的某网站,大大消耗了我的精力和耐心。同组的另一位员工直接开发了一个python脚本实现"半"自动化操作,拿来修改并且学习之后,解放双手,真的很赞!
接下来主要记录整个学习过程。

准备

需要用到:

  1. python环境
  2. selenium工具
  3. chromedriver

1.安装Anaconda

Anaconda时python的开源的包管理器和环境管理器,直接Anaconda官网下载
注意要在安装的时候选择配置环境路径。
下载完了之后,就可以在命令行里使用conda
如果要打开自带的jupyter notebook,命令行里输入:
jupyter notebook

2.安装selenium

命令行里输入:
pip install selenium
等待着安装成功

3.安装chromedriver

参考博客链接

测试准备工作是否完成(selenium的使用)

打开jupyter notebook,直接新建一个python文件,上网上拷贝了一段测试代码:

import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(8) # 设置隐式等待时间
 
driver.get("https://www.baidu.com") # 地址栏里输入网址
driver.find_element_by_xpath('//*[@id="kw"]').send_keys("胡歌") # 搜索框输入胡歌
driver.find_element_by_xpath('//*[@id="su"]').click() # 点击百度一下按钮
 
time.sleep(2) # 等待2秒
# 通过元素XPath来确定该元素是否显示在结果列表,从而判断“壁纸”这个链接是否显示在结果列表
# find_element_by_link_text当找不到此链接时报错,程序停止
driver.find_element_by_link_text('胡歌的新浪微博').is_displayed()
driver.quit()

结果日志

---------------------------------------------------------------------------

FileNotFoundError                         Traceback (most recent call last)

~Anaconda3libsite-packagesseleniumwebdrivercommonservice.py in start(self)
     71             cmd.extend(self.command_line_args())
---> 72             self.process = subprocess.Popen(cmd, env=self.env,
     73                                             close_fds=platform.system() != 'Windows',


~Anaconda3libsubprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors, text)
    853 
--> 854             self._execute_child(args, executable, preexec_fn, close_fds,
    855                                 pass_fds, cwd, env,


~Anaconda3libsubprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_start_new_session)
   1306             try:
-> 1307                 hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
   1308                                          # no special security


FileNotFoundError: [WinError 2] 系统找不到指定的文件。


During handling of the above exception, another exception occurred:


WebDriverException                        Traceback (most recent call last)

<ipython-input-3-0d997f96642b> in <module>
      1 import time
      2 from selenium import webdriver
----> 3 driver = webdriver.Chrome()
      4 driver.maximize_window()
      5 driver.implicitly_wait(8) # 设置隐式等待时间


~Anaconda3libsite-packagesseleniumwebdriverchromewebdriver.py in __init__(self, executable_path, port, options, service_args, desired_capabilities, service_log_path, chrome_options, keep_alive)
     71             service_args=service_args,
     72             log_path=service_log_path)
---> 73         self.service.start()
     74 
     75         try:


~Anaconda3libsite-packagesseleniumwebdrivercommonservice.py in start(self)
     79         except OSError as err:
     80             if err.errno == errno.ENOENT:
---> 81                 raise WebDriverException(
     82                     "'%s' executable needs to be in PATH. %s" % (
     83                         os.path.basename(self.path), self.start_error_message)


WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

报错:需要把‘chromedriver’的路径写全,修改如下:

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
driver = webdriver.Chrome("C:/Users/user47/AppData/Local/Google/Chrome/Application/chromedriver.exe")
driver.maximize_window()
driver.implicitly_wait(8) # 设置隐式等待时间
 
driver.get("https://www.baidu.com") # 地址栏里输入网址
driver.find_element_by_xpath('//*[@id="kw"]').send_keys("胡歌") # 搜索框输入胡歌
driver.find_element_by_xpath('//*[@id="su"]').click() # 点击百度一下按钮
 
time.sleep(2) # 等待2秒

最后实现打开一个新的chrome,打开百度官网,然后搜索"胡歌"的功能。

接下来就到了在工作中真正使用的时候。

某网站的数据清洗用到的python代码

数据编目、归集之后,需要进行数据清洗。
某网站整个数据清洗包括几大部分:

  1. SQL语句的修改:
  • Show tables like ‘表名’:查出来后缀名是日期的表名;此表是编目归集后的表,也就是清洗前的表。
  • Show create table 表名:查出来表结构(字段)
  • 加字段,主键、时间戳
  1. 建立模型:
  • 贴元层ODS
  • 清洗层DWD
    因为在程序开发之前,有依赖:必须有两张表:ODS_和DWD_,这两张表结构是一样的,只有名字不一样
  1. 建立程序和调度配置
  • 新建DWD_的可视化编辑程序
  • 导入清洗的工作流XML
  • 配置两个表:ODS_和DWD_
  • 选择调度配置
  • 测试

createODS部分+DWD部分

涉及到的知识

  1. chromeOptions 是一个配置 chrome 启动是属性的类。通过这个类,我们可以为chrome配置如下参数(这个部分可以通过selenium源码看到):
  • 设置 chrome 二进制文件位置 (binary_location)
  • 添加启动参数 (add_argument)
  • 添加扩展应用 (add_extension, add_encoded_extension)
  • 添加实验性质的设置参数 (add_experimental_option)
  • 设置调试器地址 (debugger_address)
  1. 元素定位的方法
  • find_element_by_id()
  • find_element_by_name()
  • find_element_by_class_name()
  • find_element_by_tag_name()
  • find_element_by_link_text()
  • find_element_by_partial_link_text()
  • find_element_by_xpath()
  • find_element_by_css_selector()
    主要使用xpath:对网页F12,选中元素后,转到html代码部分,右键copy to xpath可以得到
  1. Xpath的定位策略
    参考链接:https://www.yiibai.com/selenium/selenium-ide-locating-strategies-by-xpath.html
    需要在命令行执行下面的命令打开chrome浏览器,之后的脚本都是在这个里面执行的。

  2. 配置chrome.exe环境变量

chrome.exe --remote-debugging-port=9222 --user-data-dir="C:selenumAutomationProfile"

结果日志

  File "<ipython-input-6-6c22bdc95370>", line 1
    chrome.exe --remote-debugging-port=9222 --user-data-dir="C:selenumAutomationProfile"
    ^
SyntaxError: cannot assign to operator

如果执行不成功:需要添加环境变量:属性->高级系统设置->高级->环境变量->path->将我的chrome.exe所在的文件路径C:Usersuser47AppDataLocalGoogleChromeApplication添加进去
最后ok

# This is a sample Python script.
# coding:utf-8
import time

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
from selenium.webdriver.common.keys import Keys


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.

    #创建ODS

def createODS(tableName, table):
    chrome_options = Options()
    chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
    driver = webdriver.Chrome("C:/Users/user47/AppData/Local/Google/Chrome/Application/chromedriver.exe",
                              options=chrome_options)
    # browser.get("https://shportal.shbd.sh.cegn.cn/VueFrame/IAM-102/102")
    driver.switch_to_frame(driver.find_element_by_class_name("vue-iframe"))
     #数据开发
    driver.find_element_by_xpath("//*[@id="sidebar"]/div[2]/div[3]/ul/li[2]/a/span[2]").click()
    driver.switch_to_frame(driver.find_element_by_xpath("//*[@id="data-dev"]"))
   
    driver.find_element_by_xpath("//*[@id="sidebar"]/div[2]/div[3]/ul/li[1]/a/span[2]").click()
    driver.switch_to_frame(driver.find_element_by_xpath("//*[@id="1020129"]"))

    driver.find_element_by_xpath(
        "//*[@id="content"]/div[1]/div/div[2]/div/div/div[1]/div/div/div[2]/div[1]/div[2]/div[6]").click()
    driver.find_element_by_xpath(
        "//*[@id="content"]/div[1]/div/div[2]/div/div/div[3]/div/div/div[1]/form/div/div[6]/div/button[2]").click()
    time.sleep(3)
    driver.switch_to_frame(driver.find_element_by_xpath("//*[@id="content"]/div[1]/div/div[2]/div[2]/iframe"))
    driver.find_element_by_xpath("//*[@id="baseForm"]/div[1]/div/div/div[1]/input").click()
    time.sleep(1)
    driver.find_element_by_xpath("/html/body/div[2]/div/div[1]/ul/li[8]").click()
    driver.find_element_by_xpath("//*[@id="baseForm"]/div[3]/div/div[1]/input").send_keys("ODS_" + tableName)
    driver.find_element_by_xpath("//*[@id="baseForm"]/div[4]/div/div/input").send_keys(table)
    driver.find_element_by_xpath("//*[@id="baseForm"]/div[10]/div/div/textarea").send_keys("1111111111111111")
    driver.find_element_by_xpath("//*[@id="baseForm"]/div[7]/div/div/span/span").click()
    time.sleep(1)
    driver.find_element_by_xpath("/html/body/div[3]/ul/li").click()
    time.sleep(1)
    driver.find_element_by_xpath("/html/body/div[3]/ul[2]/li[1]").click()
    time.sleep(1)
    driver.find_element_by_xpath("//*[@id="baseForm"]/div[9]/div/div/div[1]/input").click()
    time.sleep(1)
    driver.find_element_by_xpath("/html/body/div[4]/div/div[1]/ul/li[6]").click()
    time.sleep(1)
    driver.find_element_by_xpath("//*[@id="content-operate"]/div/div/div/div/button").click()
    driver.find_element_by_xpath("//*[@id="field-table"]/div/div[2]/div/div/div[2]/div/button[4]").click()

    # 加入从本地文本复制,需要提前复制好,就差ctrl+V
    driver.find_element_by_xpath("//*[@id="content"]/div[7]/div/div[2]/div/form/div[1]/div/div/textarea").send_keys(
        Keys.LEFT_CONTROL, 'v')
    driver.find_element_by_xpath("//*[@id="content"]/div[7]/div/div[2]/div/form/div[2]/div/button").click()
    time.sleep(0.5)
    #选主键和时间戳:默认打开最后一个
    driver.find_element_by_xpath(
        "//*[@id="field-table"]/div/div[2]/div/div/div[3]/div[3]/div/table/div/tr[last()]/td[last()]").click()
    time.sleep(0.5)
    driver.find_element_by_xpath(
        "//*[@id="field-table"]/div/div[2]/div/div/div[3]/div[3]/div/table/div/tr[last()]/td[last()]/form/div[3]/div/label/div[3]").click()
    driver.find_element_by_xpath("//*[@id="content-operate"]/div/div/div/button").click()
    driver.switch_to.parent_frame()
    driver.find_element_by_xpath("//*[@id="content"]/div[1]/div/div[1]/div[2]/div[1]/div/div/span[2]").click()
    time.sleep(1)
    driver.find_element_by_xpath("//button[contains(.,'确定关闭')]").click()
def createDWD(tableName, table):
    chrome_options = Options()
    chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
    driver = webdriver.Chrome("C:/Users/user47/AppData/Local/Google/Chrome/Application/chromedriver.exe",
                              options=chrome_options)
    # browser.get("https://shportal.shbd.sh.cegn.cn/VueFrame/IAM-102/102")
    driver.switch_to_frame(driver.find_element_by_class_name("vue-iframe"))
    driver.find_element_by_xpath("//*[@id="sidebar"]/div[2]/div[3]/ul/li[2]/a/span[2]").click()
    driver.switch_to_frame(driver.find_element_by_xpath("//*[@id="data-dev"]"))
    driver.find_element_by_xpath("//*[@id="sidebar"]/div[2]/div[3]/ul/li[1]/a/span[2]").click()
    driver.switch_to_frame(driver.find_element_by_xpath("//*[@id="1020129"]"))

    driver.find_element_by_xpath(
        "//*[@id="content"]/div[1]/div/div[2]/div/div/div[1]/div/div/div[2]/div[1]/div[2]/div[2]").click()
    driver.find_element_by_xpath(
        "//*[@id="content"]/div[1]/div/div[2]/div/div/div[3]/div/div/div[1]/form/div/div[6]/div/button[2]").click()
    time.sleep(5)
    driver.switch_to_frame(driver.find_element_by_xpath("//*[@id="content"]/div[1]/div/div[2]/div[2]/iframe"))
    driver.find_element_by_xpath("//*[@id="baseForm"]/div[1]/div/div/div[1]/input").click()
    time.sleep(1)
    driver.find_element_by_xpath("/html/body/div[2]/div/div[1]/ul/li[8]").click()
    driver.find_element_by_xpath("//*[@id="baseForm"]/div[3]/div/div[1]/input").send_keys("DWD_" + tableName)
    driver.find_element_by_xpath("//*[@id="baseForm"]/div[4]/div/div/input").send_keys(table)
    driver.find_element_by_xpath("//*[@id="baseForm"]/div[10]/div/div/textarea").send_keys("1111111111111111")
    driver.find_element_by_xpath("//*[@id="baseForm"]/div[7]/div/div/span/span").click()
    time.sleep(1)
    driver.find_element_by_xpath("/html/body/div[3]/ul/li").click()
    time.sleep(1)
    driver.find_element_by_xpath("/html/body/div[3]/ul[2]/li[1]").click()
    time.sleep(1)
    driver.find_element_by_xpath("//*[@id="baseForm"]/div[9]/div/div/div[1]/input").click()
    time.sleep(1)
    driver.find_element_by_xpath("/html/body/div[4]/div/div[1]/ul/li[6]").click()
    time.sleep(1)
    driver.find_element_by_xpath("//*[@id="content-operate"]/div/div/div/div/button").click()
    driver.find_element_by_xpath("//*[@id="field-table"]/div/div[2]/div/div/div[2]/div/button[4]").click()

    # 加入从本地文本复制:要求执行之前必须把用到的SQL建表语句复制
    driver.find_element_by_xpath("//*[@id="content"]/div[7]/div/div[2]/div/form/div[1]/div/div/textarea").send_keys(
        Keys.LEFT_CONTROL, 'v')
    driver.find_element_by_xpath("//*[@id="content"]/div[7]/div/div[2]/div/form/div[2]/div/button").click()
    time.sleep(0.5)
    driver.find_element_by_xpath(
        "//*[@id="field-table"]/div/div[2]/div/div/div[3]/div[3]/div/table/div/tr[last()]/td[last()]").click()
    time.sleep(0.5)
    driver.find_element_by_xpath(
        "//*[@id="field-table"]/div/div[2]/div/div/div[3]/div[3]/div/table/div/tr[last()]/td[last()]/form/div[3]/div/label/div[3]").click()
    driver.find_element_by_xpath("//*[@id="content-operate"]/div/div/div/button").click()
    driver.switch_to.parent_frame()
    driver.find_element_by_xpath("//*[@id="content"]/div[1]/div/div[1]/div[2]/div[1]/div/div/span[2]").click()
    time.sleep(1)
    driver.find_element_by_xpath("//button[contains(.,'确定关闭')]").click()
def go(name, table):
    createODS(name, table)
    createDWD(name, table)
    #createWork(name, table)
#模型开发ODS
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    
    # chrome.exe --remote-debugging-port=9222–user-data-dir="D:selenumAutomationProfile"
    print_hi('PyCharm')
    
    #go("tablename", "TABLE")
    go("tablename_new","TABLE_new")

# See PyCharm help at https://www.jetbrains.com/help/pycharm/

结果日志

Hi, PyCharm


<ipython-input-40-ce205903ef65>:25: DeprecationWarning: use driver.switch_to.frame instead
  driver.switch_to_frame(driver.find_element_by_class_name("vue-iframe"))
<ipython-input-40-ce205903ef65>:28: DeprecationWarning: use driver.switch_to.frame instead
  driver.switch_to_frame(driver.find_element_by_xpath("//*[@id="data-dev"]"))
<ipython-input-40-ce205903ef65>:31: DeprecationWarning: use driver.switch_to.frame instead
  driver.switch_to_frame(driver.find_element_by_xpath("//*[@id="1020129"]"))
<ipython-input-40-ce205903ef65>:38: DeprecationWarning: use driver.switch_to.frame instead
  driver.switch_to_frame(driver.find_element_by_xpath("//*[@id="content"]/div[1]/div/div[2]/div[2]/iframe"))
<ipython-input-40-ce205903ef65>:80: DeprecationWarning: use driver.switch_to.frame instead
  driver.switch_to_frame(driver.find_element_by_class_name("vue-iframe"))
<ipython-input-40-ce205903ef65>:82: DeprecationWarning: use driver.switch_to.frame instead
  driver.switch_to_frame(driver.find_element_by_xpath("//*[@id="data-dev"]"))
<ipython-input-40-ce205903ef65>:84: DeprecationWarning: use driver.switch_to.frame instead
  driver.switch_to_frame(driver.find_element_by_xpath("//*[@id="1020129"]"))
<ipython-input-40-ce205903ef65>:91: DeprecationWarning: use driver.switch_to.frame instead
  driver.switch_to_frame(driver.find_element_by_xpath("//*[@id="content"]/div[1]/div/div[2]/div[2]/iframe"))

程序设计部分(批量创建程序模型)

import time

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
from selenium.webdriver.common.keys import Keys
def createWork(tableName, table):
    chrome_options = Options()
    chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
    driver = webdriver.Chrome("C:/Users/user47/AppData/Local/Google/Chrome/Application/chromedriver.exe",
                              options=chrome_options)
    # browser.get("https://shportal.shbd.sh.cegn.cn/VueFrame/IAM-102/102")
    driver.switch_to_frame(driver.find_element_by_class_name("vue-iframe"))
  
    driver.find_element_by_xpath("//*[@id="sidebar"]/div[2]/div[3]/ul/li[2]/a/span[2]").click()
    driver.switch_to_frame(driver.find_element_by_xpath("//*[@id="data-dev"]"))
    driver.find_element_by_xpath("//*[@id="sidebar"]/div[2]/div[3]/ul/li[2]").click()
    driver.switch_to_frame(driver.find_element_by_xpath("//*[@id="1020147"]"))
    driver.find_element_by_xpath(
        "//*[@id="content"]/div/div/div[2]/div/div/div[3]/div/div/div[1]/form[1]/div/div/div/button[1]").click()
    
    driver.find_element_by_xpath("//*[@id="procType"]/div/div/div[1]/input").click()
    time.sleep(1)
      #可视化编辑
    driver.find_element_by_xpath("//span[contains(.,'可视化编辑')]").click()
    driver.find_element_by_xpath("//*[@id="procName"]/div/div[1]/input").send_keys("DWD_" + tableName)
    driver.find_element_by_xpath("//*[@id="main"]/div[2]/div/div[2]/form/div[3]/div/div/input").send_keys(table)
    driver.find_element_by_xpath("//*[@id="main"]/div[2]/div/div[2]/form/div[4]/div/div/div[1]/input").click()
    time.sleep(3)
   #ri调度父节点一直在变化,如何解决?

    driver.find_element_by_xpath("/html/body/div[7]/div/div[1]/ul/li[4]").click()
    time.sleep(1)
    driver.find_element_by_xpath("//*[@id="main"]/div[2]/div/div[2]/form/div[6]/div/button[2]").click();
    time.sleep(5)
   ## xpath=//span[contains(.,'保存')]
   # driver.find_element_by_xpath("//*[@id="main"]/div[2]/div/div[2]/form/div[6]/div/button[2]").click()
    


def go(name, table):
    #createODS(name, table)
    #createDWD(name, table)
    createWork(name, table)

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    # chrome.exe --remote-debugging-port=9999–user-data-dir="$path"
    # chrome.exe --remote-debugging-port=9222–user-data-dir="D:selenumAutomationProfile"
    print_hi('PyCharm')

    #go("tablename", "TABLE")
    go("tablename_new","TABLE_new")
    
    

结果日志

Hi, PyCharm


<ipython-input-45-638c6384ac22>:15: DeprecationWarning: use driver.switch_to.frame instead
  driver.switch_to_frame(driver.find_element_by_class_name("vue-iframe"))
<ipython-input-45-638c6384ac22>:18: DeprecationWarning: use driver.switch_to.frame instead
  driver.switch_to_frame(driver.find_element_by_xpath("//*[@id="data-dev"]"))
<ipython-input-45-638c6384ac22>:20: DeprecationWarning: use driver.switch_to.frame instead
  driver.switch_to_frame(driver.find_element_by_xpath("//*[@id="1020147"]"))

目前发现的问题

  1. 创建ODS时选择下拉的Xpath总是变化导致选中不了;
    应该是定位问,但是还没有解决。
  2. 目前DWD和ODS可以建立,默认最后一个为时间戳,发现只好把时间戳在SQL中改为最后一行

扩展

  • 思考:如何实现更改名字后缀or前缀
  • 思考:如何把代码写到chrome页面的扩展程序里面,方便使用
  • 思考:能否实现SQL的自动化,利用STL?

selenium IDE Chrome下载安装与使用

为了方便调试,下载selenium IDE
教程链接

为什么Xpath定位不到

  • XPath即为XML路径语言,它是一种用来确定XML1(标准通用标记语言3的子集)文档中某部分位置的语言
  • 如果里面元素被隐藏了或者元素有变动的话,绝对定位就会出错,就会出现定位不到的情况.
  • 相对定位通常可以和id或者class-name结合使用。
    !学习链接

解决"解决元素不可交互 element not interactable" :

  • 出现元素不可交互原因可能有:
    • 点击事件太快,元素还未加载出来,可以在前面加个等待时间;
    • 元素不在页面上,比如hidden的,需要先去掉这个属性再操作;
      参考文档

      go("WS_SM_SedimentationMonitorInfo","沉降监测数据管理表")
      go("WS_SM_SectionMonitorInfo","断面监测数据管理表")
      go("WS_SM_SectionInfo","断面表")
      go("WS_SM_BeachLandSectionInfo","断面沉降监测点表")

双击进去

# 双击点进去
#双击需要导入ActionChains 类
import time

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
from selenium.webdriver.common.keys import Keys
def Doubleclink(tableName, table):
    chrome_options = Options()
    chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
    driver = webdriver.Chrome("C:/Users/user47/AppData/Local/Google/Chrome/Application/chromedriver.exe",
                              options=chrome_options)
    # browser.get("https://shportal.shbd.sh.cegn.cn/VueFrame/IAM-102/102")
    driver.switch_to_frame(driver.find_element_by_class_name("vue-iframe"))
  
    driver.find_element_by_xpath("//*[@id="sidebar"]/div[2]/div[3]/ul/li[2]/a/span[2]").click()
    driver.switch_to_frame(driver.find_element_by_xpath("//*[@id="data-dev"]"))
    driver.find_element_by_xpath("//*[@id="sidebar"]/div[2]/div[3]/ul/li[2]").click()
    driver.switch_to_frame(driver.find_element_by_xpath("//*[@id="1020147"]"))
    #对定位到的元素执行鼠标双击操作
    # 路径中带入变量path = "//android.widget.TextView[contains(@text,'"+who+"')]"
    qqq= driver.find_element_by_xpath("//span[contains(.,'"+tableName+"')]")
    ActionChains(driver).double_click(qqq).perform()
    time.sleep(2)
    #导入//*[@id="graphEditorContainer"]/div[1]/div[1]/form/div[9]/div/button/span/span
    driver.find_element_by_xpath("//span[contains(.,'导入')").click()
    time.sleep(1)
    # 加入从本地文本复制,需要提前复制好,就差ctrl+V
    driver.find_element_by_xpath("//*[@id="graphEditorContainer"]/div/div[2]/div/div[2]/textarea").send_keys(
        Keys.LEFT_CONTROL, 'v')

    
def go(name, table):
    #createODS(name, table)
    #createDWD(name, table)
    Doubleclink(name, table)

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    # chrome.exe --remote-debugging-port=9999–user-data-dir="$path"
    # chrome.exe --remote-debugging-port=9222–user-data-dir="D:selenumAutomationProfile"
    # PDXQSJQX1 pdxqsjqx1
    print_hi('PyCharm')
    go("WS_SM_SedimentationMonitorInfo","沉降监测数据管理表")
   
   
   
原文地址:https://www.cnblogs.com/zhuomoyixia/p/14174266.html