0
我正在创建一个允许用户通过Python与网站进行交互的函数。具体来说,将向用户提供要选择的选项列表,并且所选择的选项将点击相应的链接。我们假设这是我的代码:Python - 从Selenium的列表中选择一个选项
crop = input('\n\nSelect a crop: Wheat, Wetland rice, Dryland rice, Maize, Barley, Sorghum, Rye, Pearl millet, '
'Foxtail millet, Oat, Buckwheat, White potato, Sweet potato, Cassava, \n Yam and Cocoyam, Sugarcane, Sugarbeet,'
' Phaseolus bean, Chickpea, Cowpea, Dry pea, Gram, Pigeonpea, Soybean, Sunflower, Rapeseed, Groundnut, Oilpalm, '
'Olive, Jatropha, \n Cabbage, Carrot, Onion, Tomato, Banana, Citrus, Coconut, Cocoa, Cotton, Flax, Coffee, Tea, '
'Tobacco, Alfalfa, Pasture, Miscanthus, Switchgrass, Reed canary grass')
我该如何编写下一个功能,让我们根据他或她的答案点击相应的链接?我使用哪个driver.find_element_by_
代码?
我想使用的:
Select(driver.find_element_by_css_selector(crop).select_by_value(crop)
但它不工作。
如果有帮助,这对小麦链接代码:
<input id="buttonSubmit__dim_fieldcrp2=ce_whe&dimType=crp2&fieldmain=main_py_six_qdns&idPS=1e1d6e7d7ec3368cf13a68fc523d1ed4870e8b45&idAS=0&idFS=0" name="fieldcrp2=ce_whe&dimType=crp2&fieldmain=main_py_six_qdns&idPS=1e1d6e7d7ec3368cf13a68fc523d1ed4870e8b45&idAS=0&idFS=0&_passChanged=true&_eventdim" value="Wheat" onclick="return wf_click(this);" type="submit" class="linksubmit linksubmitfalse" style="border: none;">
但是,只有当我想选择小麦时才有用。如果我想选择列表中的其他项目怎么办?例如,玉米? – user2105555
我编辑了我的答案,以显示如何使用用户输入的值格式化字符串。 – mgc
非常感谢!有用!我只是在代码的末尾添加了'.click()',就像'driver.find_element_by_css_selector('input [value =“{}”]'。format(crop))。click()' – user2105555