zoukankan      html  css  js  c++  java
  • web自动化中的js操作:用js获取url、hostname、port、title等值并打印输出

    """
    实现功能:使用python+selenium+js完成,任意打开一个链接,用js获取url、hostname、port、title等值并打印
    作者:柠檬草不孤单
    """
    from selenium import webdriver
    driver=webdriver.Chrome()
    #最大化窗口
    driver.maximize_window()
    #隐式等待
    driver.implicitly_wait(5)
    #打开淘宝
    driver.get("https://www.taobao.com/?spm=a2107.1.0.0.259d11d9vireQv")
    #js获取url
    js_url="return window.location.href"
    url=driver.execute_script(js_url)
    #打印url
    print("url:"+url)
    driver.get(url)
    #打印title
    print("title:"+driver.title)
    
    #js获取hostname
    js_hostname="return window.location.hostname"
    hostname=driver.execute_script(js_hostname)
    print("hostname:"+hostname)
    
    #js获取port
    js_port="return window.location.port"
    port=driver.execute_script(js_port)
    print("port:"+port)
    
    #js获取protocol
    js_protocol="return window.location.protocol"
    protocol=driver.execute_script(js_protocol)
    print("protocol:"+protocol)
    
    #搜索框输入
    driver.find_element_by_xpath("//input[@id='q']").send_keys("衬衫")

     演示结果:

  • 相关阅读:
    php 数组
    条件语句if else ,switch ,while ,do.while
    if..else 判断中的 Boolean()转换
    wampserver 集成环境
    sublime text 安装及使用
    vue tab切换
    SVG 基础
    gitosis管理员的密钥丢失解决办法
    源码安装MySQL
    Xshell远程登录
  • 原文地址:https://www.cnblogs.com/temari/p/13629185.html
Copyright © 2011-2022 走看看