首页 > 解决方案 > 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 他没有我遇到的问题。

有人可以帮忙吗?

标签: seleniumselenium-webdriverselenium-chromedriver

解决方案


self创建驱动程序实例时您丢失了。所以而不是

driver=webdriver.Chrome(chrome_options=options,executable_path=chromedriver)

它应该是

self.driver=webdriver.Chrome(chrome_options=options,executable_path=chromedriver)

(在视频中他们正是这样做的)


推荐阅读