1

I'm new to python and trying to convert my selenium-Java scripts into selenium-python. I've been struggling to convert below code. Well, this is running successfully with Selenium-Java.

I got stuck wherein i would like to get the attribute value at a particular location and converting it to a string.

String indx=element.getAttribute("num");
int k=Integer.parseInt(indx);
element.sendKeys(""+a[k]);

How do I get that running in Python? Thank you

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){

    }
}
3
  • 1
    I won't try to do a full answer since I've never used java, but from my read of your code, you seem to be skipping over one of the best features of python: Dynamic typing. You don't need to declare data types at assignment. a=['0','p','a','s','s','w','o','r','d','1'], indx=element.getAttribute("num"), and k=int(indx). Does that help? Commented May 10, 2019 at 16:03
  • More concisely it could also be a = "0password1". Also, I think it should be .get_attribute("num") for python. Commented May 10, 2019 at 16:29
  • Thanks @G.Anderson Commented May 12, 2019 at 20:22

1 Answer 1

1

Firstly, you should learn python. It is not that difficult if you know Java already. As for the function calls nomenclature in python they are slightly different from here

I hope this works for you, I don't know selenium but this should work

# 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")

And more concisely, it would look like this:

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")
Sign up to request clarification or add additional context in comments.

5 Comments

looks like your except is indented too far
Ah yes, thank you for that. Is there a reason why the tab button doesn't work in the editor? Or am I missing something?
@AhmadMoussa, Thank you so much for your assistance on this. When i ran it, I got an error as "TypeError: cannot concatenate 'str' and 'int' objects". I then just changed element = driver.find_element_by_id("pff" + i) to element = driver.find_element_by_id("pff" + str(i)) and it worked successfully.
@Quinn, Thank you
@Abhish congrats on solving it. If this helped, consider clicking the check mark left to the answer to accept it and close this question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.