我是python的新手,正在尝试将selenium-Java脚本转换为selenium-python。我一直在努力转换下面的代码。好吧,这已在Selenium-Java上成功运行。
我陷入困境,我想在特定位置获取属性值并将其转换为字符串。
String indx=element.getAttribute("num");
int k=Integer.parseInt(indx);
element.sendKeys(""+a[k]);
如何在Python中运行它?
谢谢
char a[]={'0','p','a','s','s','w','o','r','d','1'};
for(int i=1;i<=3;i++) {
try {
WebElement element = driver.findElement(By.id("pff"+i));
if(element != null) {
String indx=element.getAttribute("num");
int k=Integer.parseInt(indx);
element.sendKeys(""+a[k]);
}
}
catch(Exception e){
}
}
最佳答案
首先,您应该学习python。如果您已经了解Java,这并不困难。至于python中的函数调用命名法,它们与here略有不同
我希望这对您有用,我不懂硒,但是应该可以
# In python comments are preceded by a hashtag
# In python you don't have to declare the type of a list
# A will be an array of the letters
a = ['0','p','a','s','s','w','o','r','d','1']
# Pay attention to your indentation, you should read up on it
# indentation is very important in python
# loop that goes from 0 to 3 (4 here because it's not inclusive)
for i in range(0, 4):
# try statement
try:
# in python you don't need to declare the type of a variable
webElement = driver.find_element_by_id("pff" + str(i))
if element != None:
# to get the attribute you call this method
indx = element.get_attribute("num")
# this is how you parse in python
k = int(indx)
element.send_keys("" + a[k])
except: #here you have to check for which error
print("An error has occurred")
更简洁地说,它看起来像这样:
a = "0password1"
for i in range(0, 4):
try:
if driver.find_element_by_id("pff" + str(i)) != None:
element.send_keys("" + a[int(element.get_attribute("num"))])
except:
print("An error has occurred")