1

I am running a selenium test with python. I am having a specific problem with the following bit of html code.

<span class="cke_label" id="cke_8_label">Source</span>

I have tried the following:

self.assert_element_present_by_class('Source button', 'cke_8_label', 'cke_label')**

here's the function:

def assert_element_present_by_class(self, description, id, name):
    sel = self.selenium
    try:
        self.assert_(sel.is_element_present("//" + id + "[contains(@class,'" + name + "')]"))
        logging.info('PASS: "' + description + '" is present')
    except Exception:
        logging.exception('FAIL: "' + description + '" is not present')

This gives me this error.......

File "Template_Designer_P37.py", line 293, in assert_element_present_by_class self.assert_(sel.is_element_present("//" + id + "[contains(@class,'" + name + "')]")) File "C:\Python27\lib\unittest\case.py", line 420, in assertTrue raise self.failureException(msg) AssertionError: False is not true

I have tried a couple of other methods, such as simply trying to assert that the id exists.

My hunch is that the problem surrounds the 'id'.

Any thoughts?

1
  • Try this: self.assert_(self.sel.is_element_present("//*[@id='" + id + "' and contains(@class,'" + name + "')]")) Commented Nov 7, 2013 at 23:21

2 Answers 2

2

You try to use incorrect xpath and insert id instead of tag name. So correct version will be

self.assert_(sel.is_element_present("//*[@id='" + id + "' and contains(@class,'" + name + "')]"))
Sign up to request clarification or add additional context in comments.

1 Comment

I think self.sel would do the task.
0

The issue is with how you’re building your XPath.

In your code:

"//" + id + "[contains(@class,'" + name + "')]"

If id = "cke_8_label"This produces:

//cke_8_label[contains(@class,'cke_label')]

That XPath is invalid, because cke_8_label is not an HTML tag name. It’s an attribute value.

Correct XPath

You want to locate an element by its id and class. The right XPath is:

//*[@id='cke_8_label' and contains(@class,'cke_label')]

Fixed Function

def assert_element_present_by_class(self, description, id, name):
    sel = self.selenium
    try:
        xpath = f"//*[@id='{id}' and contains(@class,'{name}')]"
        self.assert_(sel.is_element_present(xpath))
        logging.info(f'PASS: "{description}" is present')
    except Exception:
        logging.exception(f'FAIL: "{description}" is not present')

Even simpler

Since IDs are unique, you can just check by id:

xpath = f"//*[@id='{id}']"

Or with a CSS selector (cleaner than XPath):

css = f"#{id}.cke_label"
self.assert_(sel.is_element_present(css))

Summary: Your hunch was right — the problem was with the way the id was concatenated into the XPath. Use @id='value', not //id.

Comments

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.