因此,有一个由 javascript 生成的站点。并且每秒更新一次。我想每秒获取这些值。但是该站点会弹出一个模态框,询问“你还在这里吗”。然后你需要点击按钮。所以首先我的代码如下所示:
try:
button = browser.find_elements_by_xpath("//button[contains(@value, 'Refresh Page')]");
button.click()
rounds = rounds+1
except:
rounds = rounds+1
但是当我查看我的浏览器时,它仍然每一轮都重新加载,即使它没有显示在屏幕上也是如此。
我怎样才能让它工作,只在按钮出现在屏幕上时点击它?谢谢!
请您参考如下方法:
通过下面的代码,在按钮可用且可见时单击按钮以刷新页面
button = browser.find_elements_by_xpath("//button[contains(@value, 'Refresh Page')]");
//To check button is present on page
if (len(button)>0):
//And is displayed
if button[0].is_displayed():
button[0].click()
else:
print "button not display"
else:
print("there is not refresh page button")
或
// to check the dialog present here "show" is the class which retain by the div once the refresh button get enabled
if "show" in browser.find_element_by_id('timeout_dialog').get_attribute('class'):
browser.find_element_by_xpath("//button[contains(@value, 'Refresh Page')]").click()
else:
print "refresh button not displaying"
注意 : 按照python进行语法修正
