代码:
class Options:
def wd_options():
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
chromedriver= r"C:\webdriver\chromedriver.exe"
chrome_options= webdriver.ChromeOptions()
# 设置不加载图片
# prefs = {"profile.managed_default_content_settings.images": 2}
# chrome_options.add_experimental_option("prefs", prefs)
# 启动时设置默认语言为中文UTF-8
chrome_options.add_argument('lang=zh_CN.utf-8')
# user-agent用来模拟移动设备,设置lang和User-Agent信息,防止反爬虫检测
UserAgent= 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36'
chrome_options.add_argument('User-Agent=' + UserAgent)
serv= Service(executable_path=chromedriver, chrome_options=chrome_options)
wd= webdriver.Chrome(service=serv)
# 浏览器窗口最大化
wd.maximize_window()
testurl= 'https://www.baidu.com'
wd.get(testurl)
sleep(1)
ss= wd.find_element(By.XPATH, '//*[@id="s-top-left"]/a[1]').click()
sleep(4)
# 判断元素是否可以点击,若元素可见则返回:True ;若元素不可见则返回:False
from lib.pub_methodimport ElementJudge
# 实例化
elejdg= ElementJudge(wd)
# 调用is_element_visible方法
elejdg.is_element_visible(ss)
print(elejdg)
if __name__== '__main__':
start= Options.wd_options()