首页 > 解决方案 > 第二个按钮在 selenium python 中不起作用

问题描述

from selenium import webdriver

browser = webdriver.Chrome('C:/Users/linus/Desktop/chromedriver')

browser.get('Website')

searchBar = browser.find_element_by_id('a')
searchBar.send_keys('123456789')

button = browser.find_element_by_tag_name('button')
button.click()

我打开我的网站,有一个输入字段。我输入我的名字,然后单击验证。该网站等待来自我的手机的连接,但连接在大约 1 分钟后超时。当它超时时,会出现一个按钮,上面写着再试一次。

我的问题是当连接超时时,我无法让 python 脚本单击重试按钮,我总是必须手动进行。有没有办法解决这个问题。

标签: pythonseleniumgoogle-chromeselenium-webdriverautomation

解决方案


import time

# all your code above from below

searchBar = browser.find_element_by_id('a')
searchBar.send_keys('123456789')

## once it reaches this point

# wait 70 seconds to continue to script. its abit above your average delay
time.sleep(70)

try:
    # try and click the button the pops up and click it
    button = browser.find_element_by_tag_name('button')
    button.click()
else:
    button = browser.find_element_by_tag_name('button')
    button.click()

不确定这是否会起作用,但在这方面的一些东西......


推荐阅读