小编Yog*_*tup的帖子

如果在 Python 中使用 selenium 找不到元素,则继续执行脚本

如果在 Python 中使用 selenium 找不到元素,我无法弄清楚如何继续执行脚本。

此代码循环浏览报告,找到刷新按钮,然后单击下载按钮。唯一的问题是,有些报告没有刷新按钮。因此,如果找不到按钮,我希望脚本继续。

我对 python/selenium 还是个新手,所以这就是我发布整个代码的原因。我需要知道要插入什么才能完成这项工作!预先感谢您对这个头部撞击问题的帮助

这是 selenium 尝试点击刷新按钮的地方

browser.find_element_by_css_selector("#ctl00_PlaceHolderMain_ReportViewer1_HtmlOutputReportResults2_updateFilters_TitleAnchor").click()
Run Code Online (Sandbox Code Playgroud)

完整代码:

import time
import os
import os.path
import glob
import shutil
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import MoveTargetOutOfBoundsException
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException

files = glob.glob('/Users/me/Desktop/AUTOREPORTS/*')
for f in files:
    os.remove(f)        

open('/Users/me/Desktop/AUTOREPORTS/report.csv', "w")

for x in range(1, 73):      
    while True:    
        try:
            fp = webdriver.FirefoxProfile('C:/Users/me/Documents/FirefoxProfile')
            browser = webdriver.Firefox(fp)
            browser.get('https://websitething.com/')

            time.sleep(8)

            browser.find_element_by_id("ctl00_PlaceHolderMain_login_UserName").clear()
            browser.find_element_by_id("ctl00_PlaceHolderMain_login_UserName").send_keys("usr")
            browser.find_element_by_id("ctl00_PlaceHolderMain_login_password").clear()
            browser.find_element_by_id("ctl00_PlaceHolderMain_login_password").send_keys("paswd")
            browser.find_element_by_id("ctl00_PlaceHolderMain_login_login").click()

#gets …
Run Code Online (Sandbox Code Playgroud)

python selenium

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

标签 统计

python ×1

selenium ×1