python-selenium


selenium的使用:
    1,pip install selenium
    2,查看chrome版本。在chrome浏览器地址栏输入:chrome://version
    3,下载对应于chrome版本的chromedriver.exe。链接:https://chromedriver.storage.googleapis.com/index.html
        比如chrome版本为:104.0.5112.102 ,可以下载 104.0.5112.79的chromedriver.exe
    4,将chromedriver.exe复制到python的Script文件夹以及chrome的所在位置下。
        查看python的Script位置:cmd命令输入 where python。对比如结果为F:\towatt\Anaconda3\python.exe,
        则Script文件夹在F:\towatt\Anaconda3\下
        右键chrome,打开所在位置,粘贴chromedriver.exe
    5,搜索网页元素xpath方式。对网页某位置右键--检查,出现开创者工具后再次右键--检查,开发者工具中闪蓝色的部分即该网页元素
        对该元素右键--copy--xpath,即可获得其xpath
    6,需要搭配time.sleep()使用。等待网页内容加载
    7,鼠标点击。
        from selenium.webdriver.common.action_chains import ActionChains
        ActionChains(driver).move_by_offset(1300, 130).click().perform()#--context_click右键
        #或者move_to_element(driver.find_element_by_xpath())
    8,覆盖默认输入值。
        from selenium.webdriver.common.keys import Keys
        driver.find_element_by_xpath('/html/body/div[3]/div[1]/div[2]/div[1]/span[1]/span[1]/div/input').send_keys(Keys.CONTROL, 'a') #--即CTRL+A
        driver.find_element_by_xpath('/html/body/div[3]/div[1]/div[2]/div[1]/span[1]/span[1]/div/input').send_keys('2022-05-01')
    9,可运行,但一直报错(属于警告)。关闭方法:
        option = webdriver.ChromeOptions()
        option.add_argument('--ignore-certificate-errors')
        option.add_experimental_option("excludeSwitches",["enable-logging"])
        driver = webdriver.Chrome(options=option)
    10,报错Message: element not interactable
        表示元素定位成功,但click()失败。
        源代码:driver.find_element('a').click()
        解决方法:
        1,elem = driver.find_element('a')
             driver.execute_script('arguments[0].click();', elem)
        2,用第7点的方法
    11,路径报错Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[4]/div/div[1]/button/i"}
        1,可能是网页换窗口了,但driver没跟上,还在之前的窗口
        2,元素还没加载,加个time.sleep()
        3,别以为第一次获取的网页xpath就是永远对的,网页元素的下一次xpath可能会变
    12,找不到网页元素:no such element: Unable to locate element
        1,添加等待时间 
        2,元素路径(如xpath)是否正确
        3,打开新窗口的情况下,dirver是否切换到新窗口
        4,网页元素是否在内嵌网页iframe中
        5,打开新窗口后,driver是否重新进入iframe中
    13,切换窗口。
        1, driver.switch_to.window(driver.window_handles[-1])    #--切换到最新窗口
        2, 切换窗口找不到元素问题。首先按一般情况处理,比如加等待时间、检查xpath对不对等等。
        还需要注意的情况是,切换窗口后driver没跳进iframe中,所以找不到iframe中的元素


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值