0

I have a method to get an ancestor element of a web_element:

def find_ancestor(self, name):
    element = self.driver.find_element_by_name(name)
    ancestor = element.find_element_by_xpath("ancestor::*")
    print(ancestor)
    attr = ancestor.get_attribute("outerHTML")
    print(attr)

The output for print(ancestor) is <selenium.webdriver.remote.webelement.WebElement (session="43f99228044a37dab999a9512b5e268e", element="0.22129539497775563-3")> When I try to access and print attributes I get an error: UnicodeEncodeError: 'charmap' codec can't encode character u'\xae' in position 292: character maps to <undefined> The top of my script has # -*- coding: utf-8 -* What am I missing/doing wrong? My feeling is 'ancestor' returns differently than other xpath axes!?

background/bonus help Ideally, I want methods to get parent, preceding-sibling, following-sibling etc. But when I try such actions on a table cell preceding-sibling is not getting the previous cell (no element found), and with parent I am getting back the same element (the same table cell and not table row). I am showing python but don't mind answers in c#.

Ywapom
  • 601
  • 5
  • 18
  • Shouldn't your xpath have `/` in it? Have you tried `//*[name="foo"]/parent::*` , what does this give you? – nilesh Jun 18 '18 at 21:58
  • @nilesh makes no difference. The xpath I have is returning an element as it does if I use an xpath as you described. My understanding is that you don't have to have slashes. https://msdn.microsoft.com/en-us/library/ms256086(v=vs.110).aspx – Ywapom Jun 18 '18 at 22:31

1 Answers1

0

I changed the last line to print(attr.encode("utf-8")) which works. I don't get why that is required when I have # -*- coding: utf-8 -* at the top of my script. But I am now seeing the ancestor.

Ywapom
  • 601
  • 5
  • 18
  • 1
    check this out https://stackoverflow.com/questions/32382686/unicodeencodeerror-charmap-codec-cant-encode-character-u2010-character-m – nilesh Jun 18 '18 at 23:27