python-2.7 - 属性错误 : 'WebDriver' object has no attribute 'id'

标签 python-2.7

伙计们,这是关于 Python + Selenium 汽车的。测试。

我在尝试使用 ID 检查打开的页面上是否有元素时遇到“AttributeError”的问题。

如果我正在 checkin 标题,它对我有用,下面是我的代码部分:

    driver = webdriver.Firefox() 
    driver.get("https://accounts.google.com/ServiceLogin?sacu=1&scc=1&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&hl=ru&service=mail")
    wait = WebDriverWait(driver, 10)
    element = wait.until(EC.element_to_be_clickable((By.ID,"Email")))       
    self.assertIn("Gmail", driver.title) - works fine
    self.assertIn("Email", driver.id) - AttributeError occurred

任何人都可以帮助解决这个问题吗?

最佳答案

driverwebdriver.Firefox 的一个实例并且还代表它当前正在访问的网页。这就是为什么您可以访问 title属性。

但看起来你想要的是id页面上的元素。如果是这样,您需要先找到它。也许你正在寻找这个?

email_input = driver.find_element_by_id("Email")
self.assertIn("Email", email_input.get_attribute('id'))

如果 WebDriver 找不到元素,它将引发 NoSuchElementException。因此,如果您只想验证页面上是否有一个 id 为“电子邮件”的元素,那么您可以进行查找并断言它没有错误,如下所示:
try:
    email_input = driver.find_element_by_id("Email")
except NoSuchElementException:
    self.fail("Could not find 'Email' element on page")

关于python-2.7 - 属性错误 : 'WebDriver' object has no attribute 'id' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26087926/

相关文章:

Python 有选择地从字典中查找并打印值

python - Python 中的 Selenium Webdriver - Chrome 首选项中的文件下载目录更改

python - 在 scikit learn 中组合随机森林模型

Python urllib2 错误

python - 网络抓取以形成新闻数据库

python - 获取异常 pickle.dump

python sterling的近似程序

python-3.x - 如何让 virtualenv 运行 Python 3 而不是 Python 2.7?

python-2.7 - 如何解释 Python 2.7 中的花式循环?

python - 使用mysql在python中插入图像