1

I have an app QCalculator with qtwebdriver: https://github.com/cisco-open-source/qtwebdriver/pull/27/files

I am trying to write simple python test, that will test this calculator, here is my script:

#!/usr/bin/env python

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Remote(
    desired_capabilities=webdriver.DesiredCapabilities.FIREFOX,
    command_executor='http://127.0.0.1:9517'
)
driver.get("qtwidget://Calculator");

btn = driver.find_elements_by_xpath("//Button[@name='5']")[0]
btn.click();

This is interface of Calculator

The script is supposed to click on number "5", but I got this error:

Traceback (most recent call last):
  File "./testCalculator.py", line 12, in <module>
    btn = driver.find_elements_by_xpath("//Button[@name='5']")[0]
IndexError: list index out of range

Buttons are created like this: (in the link below on github)

 for (int i = 1; i < NumDigitButtons; ++i) {
         int row = ((9 - i) / 3) + 2;
         int column = ((i - 1) % 3) + 1;
         mainLayout->addWidget(digitButtons[i], row, column);
     }

How to change this code ("//Button[@name='5']")[0] to find the button ?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
VFbvb
  • 11
  • 5

1 Answers1

1

This error message...

    btn = driver.find_elements_by_xpath("//Button[@name='5']")[0]
IndexError: list index out of range

...implies that the List initialized through the find_elements_by_xpath() method was an empty list. So when you tried to invoke the first element of the list through the index [0] IndexError was raised.

Solution

  • A proper solution would be to construct a Locator Strategy which identifies the element uniquely. Here you can find a detailed discussion on Official locator strategies for the webdriver
  • Perhaps as per your line of code btn = driver.find_elements_by_xpath("//Button[@name='5']")[0] the tag name should have been button instead of Button.
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • hey, thanks for your answer. I found the way to find the button: btn5 = driver.find_elements_by_xpath("//Button[@text='5']")[0]. But I have other problem - I need to located QLineEdit widget, in where the result is stored. In code element created like this display = new QLineEdit("0"); I tried this in my python script: elementDisplay = driver.find_elements_by_xpath("//QLineEdit[@name='display']")[0] but it didn't work. – VFbvb Apr 22 '18 at 12:41
  • @VFbvb If you have a new question feel free to raise a new ticket, Stackoverflow volunteers will be happy to help you out. – undetected Selenium Apr 22 '18 at 12:44