selenium - Selenium 没有调用该网站
问题描述
我第一次尝试硒。我的代码如下:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
import selenium.webdriver.support.ui as ui
import selenium.webdriver.support.expected_conditions as EC
import os
import time
class expediaUnitTest():
def __init__(self):
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
dir_path=os.getcwd()
chromedriver=dir_path+"\chromedriver"
os.environ["webdriver.chrome.driver"]=chromedriver
driver=webdriver.Chrome(chrome_options=options,executable_path=chromedriver)
def timerPractice(self):
time.sleep(5)
def gotoexpedia(self):
self.driver.get("https://www.expedia.com/")
def teardown(self):
self.driver.close()
if __name__=="__main__":
obj=expediaUnitTest()
obj.gotoexpedia()
调用了一个新的 chromebrowser,但它不访问该网页。我收到错误消息:
AttributeError: 'expediaUnitTest' object has no attribute 'driver'
当我给 timePractise() 时,它工作得很好,因为在给定的秒数后浏览器会消失。但它似乎没有调用函数。
Ps:我正在关注这里给出的在线教程:https ://www.youtube.com/watch?v=zZjucAn_JYk 他没有我遇到的问题。
有人可以帮忙吗?
解决方案
self
创建驱动程序实例时您丢失了。所以而不是
driver=webdriver.Chrome(chrome_options=options,executable_path=chromedriver)
它应该是
self.driver=webdriver.Chrome(chrome_options=options,executable_path=chromedriver)
(在视频中他们正是这样做的)
推荐阅读
- c# - 有没有办法在一段时间后(不是立即)在 for 循环中执行操作?
- c - 样式检查中的神秘额外文本
- python-3.x - 如何使用python将会话参数传递给雪花?
- php - OpenTBS - 使用令牌替换重复段落?
- mysql - 如何在 Laravel 刀片中分组和计数?
- for-loop - Win Batch 嵌套返回“意外”错误
- python - 在熊猫中应用移位函数组
- asp.net - 如何在 json 文件中显示换行符?
- python - python:在同一图中绘制图像和绘图
- python - Python subprocess.Popen、.run 或 .call 在 bootsect.exe 上不起作用