在使用Selenium和Firefox的Python代码中,我有一个名为New.txt的文件中存储的报告名称列表,该文件位于我运行代码的计算机桌面上。该脚本循环显示这些名称,在网站上查找名称,单击(两次)并下载报告。唯一的问题是报告的名称是相似的。例如Jacks Report,Report,Report1,Report2,Mary Report。当它到达列表中的报告时,它会出错(抛出异常)。这是有道理的,因为代码基本上是找到三个相同的报告名称。 我想专门更改下面的问题代码,以找到确切的报告名称。当它搜索" REPORT"我希望它只识别该报告而不是REPORT1和REPORT2。
问题:
browser.find_element_by_xpath('//span[text()="' + z + '"]').click()
整个Python代码:
for x in range(0, i):
while True:
try:
x= x
with open('C:/Users/me/Desktop/New.txt') as t:
mylist = t.read().splitlines()
z=mylist[x]
print z
fp = webdriver.FirefoxProfile('C:/Users/ME/Documents/FirefoxProfile/')
browser = webdriver.Firefox(fp)
browser.get('https://Websiteofdeath.com')
browser.find_element_by_id("ctl00_PlaceHolderMain_login_UserName").clear()
browser.find_element_by_id("ctl00_PlaceHolderMain_login_UserName").send_keys("USER")
browser.find_element_by_id("ctl00_PlaceHolderMain_login_password").clear()
browser.find_element_by_id("ctl00_PlaceHolderMain_login_password").send_keys("PASSWORD")
browser.find_element_by_id("ctl00_PlaceHolderMain_login_login").click()
time.sleep(8)
browser.get('https://Websiteofdeath.com')
time.sleep(8)
ProgramManagement= browser.find_element_by_partial_link_text('Foo')
ProgramManagement.click()
ProgramManagement= browser.find_element_by_partial_link_text('Foob').send_keys(Keys.ARROW_LEFT)
time.sleep(8)
browser.find_element_by_xpath('//span[text()="' + z + '"]').click()
browser.find_element_by_xpath('//span[text()="' + z + '"]').click()
#Click is mentioned here twice because I need to...click twice!
#browser.find_element_by_xpath('//span[text()="' + z + '"]').click()
time.sleep(30)
try:
browser.find_element_by_css_selector("#ctl00_PlaceHolderMain_ReportViewer1_HtmlOutputReportResults2_updateFilters_TitleAnchor").click()
except NoSuchElementException:
pass
time.sleep(30)
browser.find_element_by_css_selector("#ctl00_PlaceHolderMain_ReportViewer1_HtmlOutputReportResults2_CSVButton_ImageAnchor > img").click()
time.sleep(30)
path = 'C:/Users/ME/Desktop/AUTOREPORTS'
count = len([g for g in os.listdir(path) if os.path.isfile(os.path.join(path, g))])
if count == x + 1: # equal
print "Made the speed"
pass
elif count != x + 2: # not equal
time.sleep(160)
print "Waiting a little bit"
pass
browser.quit()
os.rename('/Users/ME/Desktop/AUTOREPORTS/report.csv', '/Users/ME/Desktop/AUTOREPORTS/'+z+'.csv')
print(count,"of",i)
except:
browser.quit()
continue
else:
break
这是我试图循环的HTML代码(片段) - 注意文件名非常相似:
</div></li><li class="rtLI"><div class="rtMid">
<span class="rtSp"></span><img src="/_layouts/ecm360/images/ReadReports.gif" alt="Key Plan Information" class="rtImg" /><span title="Key Plan Information" class="rtIn">Key Plan Information</span>
</div></li><li class="rtLI"><div class="rtMid">
<span class="rtSp"></span><img src="/_layouts/ecm360/images/ReadReports.gif" alt="Key Plan Information with email addresses" class="rtImg" /><span title="Key Plan Information with email addresses" class="rtIn">Key Plan Information with email addresses</span>
</div></li><li class="rtLI"><div class="rtMid">
<span class="rtSp"></span><img src="/_layouts/ecm360/images/ReadReports.gif" alt="Key Plan Information with email addresses and process description" class="rtImg" /><span title="Key Plan Information with email addresses and process description" class="rtIn">Key Plan Information with email addresses and process description</span>
</div></li><li class="rtLI"><div class="rtMid">
&#13;