【发布时间】:2020-06-30 19:36:56
【问题描述】:
我有一个页面,上面有 3 个单选按钮。我希望我的代码连续单击这些按钮中的每一个,并在单击它们时显示一个值(mpn),我想获取该值。我可以为单个单选按钮编写代码,但我不明白如何创建一个循环,以便仅更改此按钮的值(值 = {1,2,3})
from selenium import webdriver
from bs4 import BeautifulSoup
driver = webdriver.Chrome(executable_path=r"C:\Users\Home\Desktop\chromedriver.exe")
driver.get("https://www.1800cpap.com/resmed-airfit-n30-nasal-cpap-mask-with-headgear")
soup = BeautifulSoup(driver.page_source, 'html.parser')
size=driver.find_element_by_xpath("//input[@class='product-views-option-tile-input-picker'and @value='2' ]")
size.click()
mpn= driver.find_element_by_xpath("//span[@class='mpn-value']")
print(mpn.text)
此外,对于每个页面,按钮的数量和名称都不同。因此,如果有任何通用解决方案可以扩展到所有页面,所有按钮,将不胜感激。谢谢!
【问题讨论】: