【问题标题】:Python - For Loop if statement skip to next iteration in SeleniumPython - For Loop if 语句跳到 Selenium 中的下一次迭代
【发布时间】:2017-09-18 17:33:14
【问题描述】:

我正在使用 selenium 遍历变量参数的不同组合并从网站下载数据。但是,for循环函数在没有数据时停止工作;我还注意到当 selenium 停止网页时包含标有“无法产生结果”的文本。因此,我想使用带有 selenium 的 if 语句来搜索“无法产生结果”,如果找到上述文本,则跳到下一个循环。一个例子是这样的:

import os
from selenium import webdriver
import zipfile
import pandas as pd
import time

for i in to_loop:
    # directories
    link = 'http://www.gaez.iiasa.ac.at/w/ctrl?
_flow=Vwr&_view=Welcome&idAS=0&idFS=0&fieldmain=main_&idPS=0'

    ## Access Chrome Driver to use selenium
    # Define Download Directory
    chrome_options = webdriver.ChromeOptions()
    prefs = {'download.default_directory': 'C:/.../Download'}
    chrome_options.add_experimental_option('prefs', prefs)
    driver = webdriver.Chrome(
        executable_path='C:/.../chromedriver.exe',
        chrome_options=chrome_options)
    driver.get(link)

    # Enter username and password
    driver.find_element_by_name('_username').send_keys(username)
    driver.find_element_by_name('_password').send_keys(password)
    driver.find_element_by_id('buttonSubmit__login').click()

    # Click on Suitability and Potential Yield link
    driver.find_element_by_name('_targetfieldmain=main_py&_...').click()

    # Click on Agro-ecological suitability and productivity link
    driver.find_element_by_name('&fieldmain=main_py&idPS=0&...').click()
    # Click on Agro-ecological suitability and productivity list
    driver.find_element_by_css_selector('input[value="
{}"]'.format(i[0])).click()
    # Click on crop link
    driver.find_element_by_css_selector("input.linksubmit[value=\"▸ 
Crop\"]").click()
    AES_and_P = i[0]

    driver.find_element_by_css_selector('input[value="
{}"]'.format(i[1])).click()
    # Click on Water Supply Link
    driver.find_element_by_css_selector("input.linksubmit[value=\"▸ Water 
Supply\"]").click()
    Crop = i[1]

    driver.find_element_by_css_selector('input[value="
{}"]'.format(i[2])).click()
    # Click on Input Level Link
    driver.find_element_by_css_selector("input.linksubmit[value=\"▸ Input 
Level\"]").click()
    Water_Supply = i[2]

    driver.find_element_by_css_selector('input[value="
{}"]'.format(i[3])).click()
    Input_Level = i[3]

    # If statement to skip to next loop if text found
    data_check = driver.find_elements_by_partial_link_text('Cannot produce 
results.')
    if data_check[0].is_displayed():
        continue

    # Click on Time Period and Select Baseline
    driver.find_element_by_css_selector("input.linksubmit[value=\"▸ Time 
Period\"]").click()
    driver.find_element_by_css_selector("input.linksubmit[value=\"1961-
1990\"]").click()
    # Click on Geographic Areas Link
    driver.find_element_by_css_selector("input.linksubmit[value=\"▸ 
Geographic Areas\"]").click()
    # Unselect all countries
    driver.find_element_by_xpath('//*[@id="fieldareaList__pln-1"]').click()
    # Close tab for Northern Africa
    driver.find_element_by_xpath('//*[@id="rg1-66-Northern 
Africa"]/span').click()
    # Wait 1 second
    time.sleep(1)
    # Click geographic area then country
    driver.find_element_by_xpath('//label[text()="{}"]/following-
sibling::span'.format(geographic_area)).click()
    driver.find_element_by_xpath('//label[text()="
{}"]'.format(country)).click()
    # Click on Map Link
    driver.find_element_by_css_selector("input.linksubmit[value=\"▸ 
Map\"]").click()
    # Download Data
    driver.find_element_by_xpath('//*[@id="buttons"]/a[4]/img').click()

    # Wait 2 seconds
    time.sleep(2)

    # Download blah blah
    path = 'C:/.../Download'
    destination_folder = 'C:/.../CSV_Files'
    file_list = [os.path.join(path, f) for f in os.listdir(path)]
    time_sorted_list = sorted(file_list, key=os.path.getmtime)
    file_name = time_sorted_list[-1]
    # decompress the zipped file here
    myzip = zipfile.ZipFile(file_name)

    # Wait 1 second
    time.sleep(1)

    myzip.extract('data.asc', destination_folder)

    # Save data.asc file as .csv and rename reflects download selections
    newfilename = country + Crop + Water_Supply + Input_Level + AES_and_P
    df = pd.read_table(os.path.join(destination_folder, 'data.asc'), 
sep="\s+", skiprows=6, header=None)
    df.to_csv(os.path.join(destination_folder, '{}.csv'.format(newfilename)))

    # Delete downloaded data.asc file
    delete_data_file = "C:/.../CSV_Files/data.asc"
    # if file exists, delete it
    if os.path.isfile(delete_data_file):
        os.remove(delete_data_file)
    else:  # Show error
        print("Error: %s file not found" % delete_data_file)

    driver.close()

但是,此代码只是在 continue 时停止函数,并没有完成代码的下载部分,而是遍历循环的其余部分。知道如何解决这个问题吗?另外,如果问题令人困惑,请告诉我。

【问题讨论】:

  • 您需要一个可验证的最小示例,因为不清楚您对代码中的 cmets 的意图是什么。
  • 此外,如果您在 continue 语句之后编写代码并且循环“继续”,那么您将不会运行 continue 语句之后的内容,因为设计会跳过循环的其余部分,您的问题并且代码中的 cmets 似乎暗示您希望在 continue 发生后发生下载数据,这没有任何意义。
  • 当然,我会进行编辑。关于您的第二条评论,cmets 明确表示:如果找到特定文本,则转到下一个迭代;否则,继续循环的其余部分并下载数据。

标签: python selenium for-loop if-statement


【解决方案1】:

在我们在 cmets 中讨论之后,我相信我至少看到了您的部分问题。 python continue keyword 并不意味着“继续控制流的其余部分”,而是意味着“继续循环的下一次迭代,之后跳过所有内容”。

例如,在以下一段python代码中:

a = []
for i in range(10):
    if i == 5:
        continue
    a.append(i)

print(a)

结果是:

[0, 1, 2, 3, 4, 6, 7, 8, 9]

而不是,使用你以前的逻辑

[5]

因此,要在 continue 上修复您的代码,您需要翻转逻辑以便在条件不成立时跳过,例如:

 if not data_check[0].is_displayed():
     continue

虽然我个人从未遇到过与您犯同样错误的人,但我当然强调,从语义上讲,“继续”指继续执行程序的下一部分似乎更有意义。 Python 在continue 中的选择很大程度上来自continue 作为关键字particularly in C 的历史用法。在这种情况下,我们可以将continue 看作更多是将break 语句扩展为“跳过”。

【讨论】:

  • 感谢您的解决方案以及告知我continue的功能
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-04
  • 2015-11-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多