相关疑难解决方法(0)

webdriver等待多个元素之一出现

有没有办法让a webDriverWait等待一些元素出现,并根据出现的元素采取相应的行动?

目前我WebDriverWait在try循环中执行一个操作,如果发生超时异常,我会运行替代代码,等待另一个元素出现.这看起来很笨拙.有没有更好的办法?这是我(笨拙)的代码:

try:
    self.waitForElement("//a[contains(text(), '%s')]" % mime)
    do stuff ....
except TimeoutException:
    self.waitForElement("//li[contains(text(), 'That file already exists')]")
    do other stuff ...
Run Code Online (Sandbox Code Playgroud)

它需要等待整整10秒才能查看该文件是否已存在于系统上的消息.

该函数waitForElement只执行了许多WebDriverWait调用:

def waitForElement(self, xPathLocator, untilElementAppears=True):
    self.log.debug("Waiting for element located by:\n%s\nwhen untilElementAppears is set to %s" % (xPathLocator,untilElementAppears))
    if untilElementAppears:
        if xPathLocator.startswith("//title"):
            WebDriverWait(self.driver, 10).until(lambda driver : self.driver.find_element_by_xpath(xPathLocator))
        else:
            WebDriverWait(self.driver, 10).until(lambda driver : self.driver.find_element_by_xpath(xPathLocator).is_displayed())
    else:   
        WebDriverWait(self.driver, 10).until(lambda driver : len(self.driver.find_elements_by_xpath(xPathLocator))==0)
Run Code Online (Sandbox Code Playgroud)

有人建议以更有效的方式完成这项工作吗?

python selenium webdriver wait selenium-webdriver

10
推荐指数
1
解决办法
5011
查看次数

在继续运行之前,如何让Selenium/Python等待用户登录?

我正在尝试在Selenium/Python中运行一个脚本,在脚本的其余部分可以运行之前需要在不同的位置登录.有没有办法告诉脚本暂停并在登录屏幕上等待用户手动输入用户名和密码(可能是在继续脚本之前等待页面标题更改的东西).

到目前为止这是我的代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import unittest, time, re, getpass

driver = webdriver.Firefox()
driver.get("https://www.facebook.com/")

someVariable = getpass.getpass("Press Enter after You are done logging in")

driver.find_element_by_xpath('//*[@id="profile_pic_welcome_688052538"]').click()
Run Code Online (Sandbox Code Playgroud)

python selenium

9
推荐指数
2
解决办法
2万
查看次数

标签 统计

python ×2

selenium ×2

selenium-webdriver ×1

wait ×1

webdriver ×1