Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium显式等待在Python中无法正常工作_Python_Selenium - Fatal编程技术网

Selenium显式等待在Python中无法正常工作

Selenium显式等待在Python中无法正常工作,python,selenium,Python,Selenium,我试着爬到头旗转盘上练习。我使用WebDriverWait等待元素在单击触发器后可见,但无法正常工作。这是我的密码 # ChromeDriver driver.get("https://www.python.org/") hBannerNav = driver.find_elements_by_xpath( '//ol[@class="flex-control-nav flex-control-paging"]/li/a') for i in range(len(hBannerNav

我试着爬到头旗转盘上练习。我使用
WebDriverWait
等待元素在单击触发器后可见,但无法正常工作。这是我的密码

# ChromeDriver
driver.get("https://www.python.org/")

hBannerNav = driver.find_elements_by_xpath(
    '//ol[@class="flex-control-nav flex-control-paging"]/li/a')

for i in range(len(hBannerNav)):
    print(hBannerNav[i].text)
    hBannerNav[i].click()
    try: 
        self.wait.until(EC.visibility_of_element_located(
            (By.XPATH, '//ul[@class="slides menu"]/li[{}]'.format(i + 1))))
        h1 = driver.find_element_by_xpath(
            '//ul[@class="slides menu"]/li[{}]/div/h1'.format(i + 1))
        print(h1.text) 

        # if add a sleep the crawler will work properly and smoothly,
        # but I want to use WebDriverWait only.
        # sleep(1) 

    except Exception as e:
        print('error', e)
以下是日志:

# without sleep
1
Functions Defined
2
Compound Data Types
3
error Message:

4
Quick & Easy to Learn
5
All the Flow You’d Expect # wait for a long time but still crawl it

# use sleep
1
Functions Defined
2
Compound Data Types
3
Intuitive Interpretation
4
Quick & Easy to Learn
5
All the Flow You’d Expect

使用位于的所有元素的存在

# the results by using 
h1 = self.wait.until(EC.presence_of_all_elements_located(
    (By.XPATH, '//ul[@class="slides menu"]/li[{}]/div/h1'.format(i + 1))))[0]

1
Functions Defined
2
Compound Data Types
3

4

5

添加第二次等待-等待
/div/h1

h1 = self.wait.until(EC.visibility_of_element_located(
        (By.XPATH, '//ul[@class="slides menu"]/li[{}]/div/h1'.format(i + 1))))
在这种情况下,它被放入html的时间可能比其父代晚一点


确实返回匹配的元素,因此
h1
将具有所需的值。

我加载了您的代码并对其进行了一次旋转。你基本上做对了;问题是这个
幻灯片菜单
元素有点奇怪。切换幻灯片时,会出现一种只需几分之一秒的淡入效果。在此期间,感兴趣的
li
/
h1
被视为“可见”,但滑动按钮没有响应!尝试在淡入淡出效果期间自己单击它们。什么也没发生

在使用Selenium时,我经常遇到这些小的、意想不到的时间问题,解决方案因情况而异

通常我会检查按钮是否可点击,但这里的问题不是可点击性

在这里,我通过等待上一张幻灯片的隐形效果来实现它:

for i in range(len(hBannerNav)):
    print(hBannerNav[i].text)
    hBannerNav[i].click()
    # We don't wait if i == 0 because in that case, there's no previous slide
    if i > 0:
        WebDriverWait(driver, 3).until(
            EC.invisibility_of_element_located((By.XPATH, '//ul[@class="slides menu"]/li[{}]'.format(i))))
    h1 = driver.find_element_by_xpath(
        '//ul[@class="slides menu"]/li[{}]/div/h1'.format(i + 1))
    print(h1.text)

可能还有其他可能更好的方法来解决这个时间问题,但希望这足以让您摆脱困境。

我使用了项目中所有元素的存在,这对我来说很好。您可能想试试。您可以尝试添加隐式等待,它将轮询dom以查找元素,直到给定的时间。要在创建驱动程序后执行此操作,请添加驱动程序。隐式等待(以秒为单位的等待时间)查看实际错误会有所帮助。尝试删除
Try/except
,这样您就可以看到完整的错误。@mblakesley这是一个TimeoutException。@kerwei这对我不起作用,结果会在我的帖子上更新。看起来不错,尽管我认为您不再需要第一次等待。无论我是保留两次还是只保留第二次,都不起作用。结果是一样的。