Python selenium 操作浏览器时元素不存在和display:none时的操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/haeasringnar/article/details/85118390

1、安装好你的selenium

pip install selenium

如果你使用的是pantomjs,需要安装指定版本的selenium

pip install selenium==2.48.0

2、看代码,注意我这里是一个真实项目用到的,实际过程中需要自己先定位元素

from selenium import webdriver
import time, datetime

class MyAutoDriver(object):
    def __init__(self):
        # 使用无界面模式
        # chrome_options = webdriver.ChromeOptions()
        # chrome_options.add_argument('--headless')
        # self.brower = webdriver.Chrome(r'/home/aea/chromedriver',chrome_options=chrome_options)
        # 使用有界面模式
        self.brower = webdriver.Chrome(r'/home/aea/chromedriver')
        # 使用 phantomjs
        # self.brower = webdriver.PhantomJS(phantomjs_path)
        self.username = 'your name'
        self.password = 'your password'

    def isElementExist(self,id):
        try:
            self.brower.find_element_by_id(id)
            return True
        except:
            return False

    def auto_run(self):
        print("开始自动化操作...")
        start_time = time.time()
        self.brower.get("https://services.police.govt.nz/vetting")
        while True:
            is_login = self.isElementExist('logon')
            if is_login:
                break
        username_input = self.brower.find_element_by_id('logon:username:input')
        password_input = self.brower.find_element_by_id('logon:password:input')
        username_input.send_keys(self.username)
        password_input.send_keys(self.password)
        self.brower.find_element_by_id('logon:logon').click()
        time.sleep(1)
        self.brower.get("https://services.police.govt.nz/vetting")
        while True:
            agencyRow_list = self.brower.find_elements_by_class_name("agencyRow")
            if len(agencyRow_list) != 0:
                break
        while True:
            Open_Agency = self.brower.find_element_by_xpath(r'//span[text()="Open Agency"]').is_displayed()
            if Open_Agency:
                time.sleep(1)
                break
        self.brower.find_element_by_xpath(r'//span[text()="Open Agency"]').click()
        while True:
            success_list = self.brower.find_elements_by_class_name("alert-success")
            if len(success_list) != 0:
                break
        # 先判断元素是否存在
        while True:
            Submit_Request_list = self.brower.find_elements_by_xpath(r'//a[text()="Submit Request"]')
            print(Submit_Request_list)
            if len(Submit_Request_list) != 0:
                break
        while True:
            Submit_Request = self.brower.find_element_by_xpath(r'//a[text()="Submit Request"]').is_displayed()
            print(Submit_Request)
            if Submit_Request:
                break
        time.sleep(1)
        self.brower.find_element_by_xpath(r'//a[text()="Submit Request"]').click()
        # 判断id为questionsAnswers的元素是否存在
        while True:
            is_Answers = self.isElementExist('questionsAnswers')
            if is_Answers:
                break
        # 判断 bodyContainer 元素是否隐藏,直到不隐藏才会继续处理
        while True:
            is_bodyContainer = self.brower.find_element_by_id('bodyContainer').is_displayed()
            # print(is_bodyContainer)
            if is_bodyContainer:
                break
        # 判断 questionsAnswers 元素是否隐藏,直到不隐藏才会继续处理
        while True:
            is_questionsAnswers = self.brower.find_element_by_id('questionsAnswers').is_displayed()
            # print(is_questionsAnswers)
            if is_questionsAnswers:
                break
        # 判断 predefinedRoles 元素是否隐藏,直到不隐藏才会继续处理
        while True:
            is_predefinedRoles = self.brower.find_element_by_id('predefinedRoles').is_displayed()
            print(is_predefinedRoles)
            if is_predefinedRoles:
                break
        self.brower.find_element_by_xpath(r'//span[text()="Caregiving - children and youth"]').click()
        # 判断页面是否加载完毕,是否存在元素 //div[@id="question1"]  直到元素存在,开始继续往下执行
        while True:
            question1_list = self.brower.find_elements_by_xpath(r'//div[@id="question1"]')
            print(question1_list)
            if len(question1_list) != 0:
                break
        # 当元素 //div[@id="question1"] 加载进来之后,再来判断元素是否被隐藏,如果没有继续往下执行,否则继续等待
        while True:
            is_question1 = self.brower.find_element_by_xpath(r'//div[@id="question1"]').is_displayed()
            print(is_question1)
            if is_question1:
                break
        self.brower.find_element_by_xpath(r'//strong[text()="Yes"]').click()
        self.brower.find_element_by_xpath(r'//div[@id="question1"]//a[text()="NEXT "]').click()
        while True:
            is_question2 = self.brower.find_element_by_xpath(r'//div[@id="question2"]').is_displayed()
            print(is_question2)
            if is_question2:
                break
        self.brower.find_element_by_xpath(r'//strong[text()="Paid"]').click()
        self.brower.find_element_by_xpath(r'//div[@id="question2"]//a[text()="NEXT "]').click()
        while True:
            is_question3 = self.brower.find_element_by_xpath(r'//div[@id="question3"]').is_displayed()
            print(is_question3)
            if is_question3:
                break
        self.brower.find_element_by_xpath(r'//strong[text()="Optional Police Vet"]').click()
        self.brower.find_element_by_xpath(r'//div[@id="question3"]//a[text()="NEXT "]').click()
        time.sleep(1)
        print(self.brower.current_url)
        # 先判断最终的表单是否存在
        while True:
            requestFormContainer_list = self.brower.find_elements_by_xpath(r'//div[@id="requestFormContainer"]')
            print(requestFormContainer_list)
            if len(requestFormContainer_list) != 0:
                break
        # 判断最终的表单 display是否为block
        while True:
            is_requestFormContainer = self.brower.find_element_by_id('requestFormContainer').is_displayed()
            print(is_requestFormContainer)
            if is_requestFormContainer:
                break
        choose_list = self.brower.find_elements_by_xpath(r'//span[text()="Choose One"]')
        other_list = self.brower.find_elements_by_xpath(r'//option[text()="Other"]')
        print(choose_list,other_list)
        choose_list[0].click()
        other_list[0].click()
        choose_list[1].click()
        other_list[1].click()
        self.brower.quit()
        print('结束,花费时间(s):',time.time() - start_time)

new_driver = MyAutoDriver()
new_driver.auto_run()
展开阅读全文

没有更多推荐了,返回首页