【问题标题】:Selenium Python - Explicit waits not workingSelenium Python - 显式等待不起作用
【发布时间】:2018-01-18 02:43:34
【问题描述】:

在等待页面呈现 js 时,我无法显式等待工作,因此我不得不使用 time.sleep() 以使代码按预期工作。

我阅读了文档,但仍然无法使其正常工作。 http://selenium-python.readthedocs.io/waits.html

带有 time.sleep() 的注释掉的代码部分按预期工作。 WebDriverWait 部分运行但不等待。

from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException

driver = webdriver.Chrome()
url = "https://www.target.com/"

# tells the driver to wait up to 10 seconds before timing out
# for data that will be loaded on the screen
DELAY = 10
driver.implicitly_wait(DELAY)
SLEEP_TIME = 1

# navigate to the page
driver.get(url)
time.sleep(SLEEP_TIME)

try:

    WebDriverWait(driver, DELAY).until(EC.visibility_of_element_located((By.XPATH, """//*[@id="js-toggleLeftNav"]/img"""))).click()
    WebDriverWait(driver, DELAY).until(EC.visibility_of_element_located((By.XPATH, """//*[@id="5"]"""))).click()
    WebDriverWait(driver, DELAY).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "#leftNavigation > ul:nth-child(2)")))
    """
    # opens up the side bar javascript
    driver.find_element_by_xpath("""//*[@id="js-toggleLeftNav"]/img""").click()
    time.sleep(SLEEP_TIME)

    # clicks on browse by category
    driver.find_element_by_xpath("""//*[@id="5"]""").click()
    time.sleep(SLEEP_TIME)

    # gets all the category elements
    items = driver.find_element_by_css_selector("#leftNavigation > ul:nth-child(2)").find_elements_by_tag_name("li")
    time.sleep(SLEEP_TIME)
    """
    # gets the hyperlink and category name but the first and the last,
    # since the first is back to main menu and the last is exit
    category_links = {}
    for i in range(1, len(items) - 1):
        hyperlink = items[i].find_element_by_tag_name('a').get_attribute('href')
        category_name = items[i].text
        category_links[category_name] = hyperlink

    print(category_links)

except:
    print("Timed out.")

【问题讨论】:

  • 我在 python 中为 selenium 编写了一个框架,可以处理这些繁琐的任务,官方绑定有点时髦,因为它们在大型网站/应用程序的工作流程方面感觉有点过时。如果您想查看,可以查看github.com/neetjn/py-component-controller

标签: python selenium time web-scraping


【解决方案1】:

此版本成功加载站点,等待其呈现,然后打开侧边菜单。注意 wait.until 方法是如何成功使用的,等待页面加载。您应该能够将下面的模式与您的其余代码一起使用来实现您的目标。

代码

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
driver.get("https://www.target.com/")

wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "#leftNavigation > ul:nth-child(2)")))
button = driver.find_element_by_xpath("""//*[@id="js-toggleLeftNavLg"]""")
button.click()
time.sleep(5)


driver.quit()

【讨论】:

  • 运行 button.click() "selenium.common.exceptions.ElementNotVisibleException: Message: element not visible" 时仍然出现元素不可见错误
  • @JonathanIshii 仔细检查 xpath id,你得到的页面版本可能与我不同。
  • 啊,我明白了。每次调用 click() 时,我都需要调用 wait.until,因为 js 重新加载了页面。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2021-08-16
  • 2022-06-22
  • 1970-01-01
  • 2015-10-09
  • 1970-01-01
  • 1970-01-01
  • 2013-11-15
  • 1970-01-01
相关资源
最近更新 更多