Python 在使用 dropDown.options[].text 时读取 Select Element/Website 下拉菜单的空字符串

Python reading empty strings for Select Element/Website Dropdown menu when using dropDown.options[].text

提问人:M Grant 提问时间:11/16/2023 最后编辑:BarmarM Grant 更新时间:11/16/2023 访问量:24

问:

我正在编写一个网络抓取代码,用于从美国农业部网络土壤调查网站抓取 PDF。有一个包含 for 循环的代码块。for 循环的目的是遍历“聚合方法”下拉菜单中的“深度到限制层”选项,然后选择“主导条件”。还有另一个 for 循环可以正常工作,并执行类似操作以遍历“限制类型”下拉菜单并选择每个可用的限制。

以下是每个下拉列表的元素代码:

正常工作的那个:

<select name="Restriction_Kind" id="Restriction_Kind_00000" size="1" label="Restriction Kind">
  <option value="Fragipan" selected="selected">
    Fragipan
  </option>
  <option value="Lithic bedrock">
    Lithic bedrock
  </option>
  <option value="Paralithic bedrock">
    Paralithic bedrock
  </option>
</select>

不起作用的那个:

<select name="Aggregation_Method" id="Aggregation_Method_00000" size="1" label="Aggregation Method">
  <option value="Dominant Condition">
    Dominant Condition
  </option>
  <option value="Dominant Component" selected="selected">
    Dominant Component
  </option>
  <option value="Weighted Average">
    Weighted Average
  </option>
  <option value="Minimum or Maximum">
    Minimum or Maximum
  </option>
</select>

这是我的代码,其中包含两个 for 循环:

dropDown1 = Select(driver.find_element(By.NAME,'Restriction_Kind'))
options = dropDown1.options
numOptions = range(len(options))

dropDown2 = Select(driver.find_element(By.NAME, 'Aggregation_Method'))

#Start counter
i = 0
optionsList = []

#Define a function to loop through the Aggregation methods to select "Dominant Condition"
def aggregationMethod():
    dropDown2 = Select(driver.find_element(By.NAME,'Aggregation_Method'))
    methods = dropDown2.options
    numMethods = range(len(methods))

    j = 0
    methodsList = []

    for m in numMethods:
        aggMethod = dropDown2.options[j].text
        methodsList.append(aggMethod)

        j = j + 1

    dropDown2.select_by_visible_text(methodsList[0])

#Loop through options to print the Individual Restrictive Layers PDfs
for n in numOptions:
    restriction = dropDown1.options[i].text
    optionsList.append(restriction)

    dropDown1.select_by_visible_text(optionsList[i])

    aggregationMethod()

   
driver.find_element(By.XPATH,'/html/body/div[3]/div/table/tbody/tr[2]/td/div/table/tbody/tr[2]/td/div/table/tbody/tr[2]/td[1]/div[2]/div[2]/div[7]/div[2]/div[4]/div[2]/div/form/table/tbody[3]/tr[6]/td[2]/table/tbody/tr[2]/td[2]/label').click()

    driver.find_element(By.XPATH, '//*[@id="ParameterForm50ViewRating_bottom"]').click()
    time.sleep(5)

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="controlbarprintbibid_unfold"]'))).click()
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="controlbarprintbibid_submit_button"]'))).click()
    time.sleep(5)

    mainPage = driver.window_handles[0]
    pdfViewer = driver.window_handles[1]

    driver.switch_to.window(pdfViewer)

    url = driver.current_url
    html = urllib.request.urlopen(url)
    fileName = projectName + '_Depth_to_Restrictive_Layer_' + optionsList[i] + '.pdf'
    pdf = open(fileName,'wb')
    pdf.write(html.read())
    html.close()
    pdf.close()

    #Increment Counter
    i = i + 1

    driver.switch_to.window(mainPage)


# Exit for loop and print the success statement
print('07 Depth to INDIVIDUAL Restrictive Layers PDFs Created')
time.sleep(5)

当代码“正常运行”时 变量 methodsList 应成为包含 ['主导条件'、'主导成分'、'加权平均值'、'最小值或最大值'] 的列表

我希望代码能够从较大的代码文件中为“聚合方法”下拉框选择“主导条件”。相反,只有当我将该部分代码分离到不同的“test.py”文件中时,它才能正常运行。

这向我表明代码编写正确。因此,我需要帮助弄清楚为什么它不能在更大的代码文件中正常运行。

感谢您的帮助!

python selenium-webdriver 网页抓取

评论

0赞 Barmar 11/16/2023
在 中,如果您只关心第一个选项的文本,那么遍历所有选项有什么意义呢?aggregationMethod()
0赞 Barmar 11/16/2023
缩进是否正确?如果它应该在循环内,则需要缩进。driver.find_element(By.XPATH,...)for
0赞 M Grant 11/16/2023
是的,缩进是实际的代码。由于某种原因,当我将代码粘贴到本网站上以上传我的问题时,它显示不正确。
0赞 M Grant 11/16/2023
@Barmar我明白你在说什么。我只尝试了一个 for 循环,因为当我尝试单独/没有 for 循环时,它不起作用dropDown2.select_by_visible_text('Dominant Condition')。
0赞 M Grant 11/16/2023
正如我所提到的,问题在于从更大的代码文件中正确执行代码。只有当我将这个代码块分成一个 test.py 文件时,它才会正确执行。

答: 暂无答案