我目前正在做一个项目。我希望它选择 1970 - 1999 之间的年份。应该随机选择日期和月份。已经根据 XPath 等尝试过它。效果不是很好。
网站是payoneer.com/Login Form date picker
birthselection = driver.find_elements_by_xpath("//*[@id=ui-datepicker-div]/div/div/select[1]/option[5]']").click()
最佳答案
import random
// Choose a random number from 0 to 29 and save it under randomNumber element
randomNumber = random.randint(0, 29)
// then I pass the randomNumber value inside the xpath of the option (instead of a static number like 5 or 10, you have to insert dynamically the randomNumber)
birthselection = driver.find_elements_by_xpath("//*[@id=ui-datepicker-div]/div/div/select[1]/option['+str(randomNumber)+']']").click()
首先,您必须导入随机包。然后你必须执行随机函数(选择一个从 0 到你想要的限制的随机数。这个例子是 29 因为你有 29 年的时间来选择(插入 str() 以便将你的 int 结果解析成一个字符串) .
在随机函数返回值后,您必须保存该值并将其插入到您喜欢的 XPath 中。例如,如果是 15,您将从列表中选择第 16 个元素。
如果您需要另外搜索它,您也可以从这里查看文档:https://docs.python.org/2/library/random.html
关于python - 如何让 Selenium 在单选按钮python上进行随机选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57734331/