Search code examples
pythonseleniumexecute-script

set display block to an element found by xpath


I found the elements I'm interested in:

LINKS = (By.PARTIAL_LINK_TEXT, "link_to")
links = self.browser.find_elements(*self.LINKS)

They now have display set for None and I'd like to show them:

for link in links:
  self.browser.execute_script("style.display = 'block';", link)

But gives me the js error style is undefined. I've tried some things like link.style.display or argument.style.display but I don't really understand how that should work. Could you help?


Solution

  • You need to use the element

    self.browser.execute_script('arguments[0].style.display = "block";'), link)
    

    Or if you are not sure style attribute exist use setAttribute()

    self.browser.execute_script('arguments[0].setAttribute("style", "display:block");'), link)