python中的selenium,工作中的一次实践



from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains
import time
with open('C:/Users/****/Desktop/input.txt','r') as file:
    content=[line.strip() for line in file.readlines()]
driver=webdriver.Chrome()
driver.set_window_size(1920, 1080)
#登录页面
driver.get("*********")
#输入账号
ele=driver.find_element_by_id("id_corpid")
ele.send_keys("***")
#输入密码
ele=driver.find_element_by_id("id_corppw")
ele.send_keys("***")
#点击登录
ele=driver.find_elements_by_xpath("//button[@class='btn btn-primary']")[1]
ele.click()
time.sleep(1)
def func(id):
    #在搜索框中输入
    ele=driver.find_element_by_xpath('//form[@class="smart-form mod-form ng-pristine ng-valid"]//input|//form[@class="smart-form mod-form ng-valid ng-dirty"]//input')
    ele.clear()
    #输入id
    ele.send_keys(id)
    #点击搜索
    ele=driver.find_element_by_xpath("//form[@class='smart-form mod-form ng-valid ng-dirty']/button[@type='submit']")
    ele.click()
    time.sleep(1)
    print(driver.find_element_by_xpath("//td[@class='mod-operate']/preceding-sibling::td[1]").text)
    if driver.find_element_by_xpath("//td[@class='mod-operate']/preceding-sibling::td[1]").text!='删除':
        #点击产品订阅
        #模拟鼠标的点击,该元素直接搜索可能搜不到
        ActionChains(driver).move_to_element(driver.find_element_by_xpath("//td[@class='mod-operate']/button[2]")).click().perform()
        zz=ele.find_element_by_xpath('//div[@class="modal in"]//nbot-select[@label="状态"]//select[@class="form-control ng-pristine ng-valid"]|//div[@class="modal in"]//nbot-select[@label="状态"]//select[@class="form-control ng-valid ng-dirty"]')
        ActionChains(driver).move_to_element(zz).click().perform()
        s1=Select(driver.find_element_by_xpath('//div[@class="modal in"]//nbot-select[@label="状态"]//select'))
        s1.select_by_value('0')
        ele=driver.find_elements_by_xpath('//footer/button[@type="submit"]')[2]
        ele.click()
    else:
        with open("C:/Users/****/Desktop/output.txt",'a') as file:
            file.write('\n'+id)


for id in content:
    func(id)


following-sibling::div[1] 表示距离当前节点的最近的div弟弟(哥哥指的是,该节点的上方的兄弟节点)

preceding-sibling::div[1]表示距离当前节点的最近的div哥哥(弟弟指的是,该节点的下放的兄弟节点)

猜你喜欢

转载自blog.csdn.net/hsc_1/article/details/80431029