Python使用selenium模拟点击(二)

本篇文章是接着第一篇文章讲的

具体可看第一篇:https://www.cnblogs.com/whatarey/p/10477754.html

要实现功能》搜索完毕,自动点击

这个功能做的停操蛋的,(忍不住想骂人)

按照我的做好,F12看看第一个a标签class  或者id,然后使用

find_element_by_id找到,点击就可以了
可是,他没有id,没有class 也没有name
没办法了,假如遇到这种情况,
find_element_by_xpath 使用这个
然后
("div[@id='1']/h3/a").click()


卧槽,他报错,说找不到指定的位置~~这很尴尬,然后又重新弄
看文档找到了一个 get_attribute 获取方法
结果也没用~~死活报错~~死活找不到

后面又找到一个办法
先引用from selenium.webdriver.common.action_chains import
ActionChains(seleniumGoo).move_by_offset(x,y).click().perform()
鼠标左键点击
ActionChains(seleniumGoo).move_by_offset(x, y).context_click().perform()  # 鼠标右键点击
我去
,我就用这个办法做出来的


ActionChains(seleniumGoo).move_by_offset(-480, 126).click().perform()  # 鼠标左键点击x坐标,y坐标


demo:
# coding:utf8

from   selenium  import   webdriver
import time

from selenium.webdriver.common.action_chains import ActionChains

def  Mian():
    seleniumGoo=webdriver.Chrome()
    seleniumGoo.get("https:www.baidu.com")
    seleniumGoo.find_element_by_xpath()
    #seleniumGoo.find_element_by_id("kw")
    seleniumGoo.find_element_by_id("kw").send_keys("Cgrain博客园")
    seleniumGoo.find_element_by_id('su').click()
    time.sleep(2)
    ActionChains(seleniumGoo).move_by_offset(-480, 126).click().perform()  # 鼠标左键点击, 200为x坐标, 100为y坐标
    #time.sleep(2)
    #("div[@id='1']/h3/a").click()
    #ActionChains(seleniumGoo).move_by_offset(-480, 126).context_click().perform()  # 鼠标右键点击
    time.sleep(20)






if __name__ == "__main__":
    Mian()





End,脱坑,觉得好的话点个关注+赞哦
原文地址:https://www.cnblogs.com/whatarey/p/10478192.html