0

Keep bumping into the same problem: when I use Selenium (Python), I often need to find the element to send_keys() to. E.g. on ebay front webpage, need to search for some item.

Each time I end up tring all the classes/frames around what I need (sometimes with ChroPath) and one of them works. Is there a simpler and more systematic way to find the element corresponding to search tab to send keys to?

LazyCat
  • 496
  • 4
  • 14

2 Answers2

0

Well one thing you could try is using hasattr method to check if the element has a particular attribute or not. From the official documentation about hasattr

hasattr(object, name) The arguments are an object and a string. The result is True if the string is the name of one of the object’s attributes, False if not. (This is implemented by calling getattr(object, name) and seeing whether it raises an AttributeError or not.)

if hasattr(element, 'send_keys'):
    # do something
Sahil
  • 1,387
  • 14
  • 41
  • Thanks, I've just tried a candidate - it returns True on has_attr(element, 'send_keys), but then I get the usual error: selenium.common.exceptions.ElementNotInteractableException: Message: Element – LazyCat Jun 03 '20 at 19:13
  • 1
    well thats a new error which for me is progress, take a look at this question here. https://stackoverflow.com/questions/49864965/org-openqa-selenium-elementnotinteractableexception-element-is-not-reachable-by – Sahil Jun 03 '20 at 19:19
  • also as the answer does get you forward in your problem, can you mark it as correct? – Sahil Jun 03 '20 at 19:23
  • The link to the other discussion is pretty useful though – LazyCat Jun 03 '20 at 19:45
0

No, you shouldn't try to invoke send_keys() on random elements on a webpage, e.g. ebay homepage.

Ideally, the WebElements with whom you would interact or invoke send_keys() should be part of your tests and should also be a part of the Test Plan.

While you execute your tests, in the due coarse you may encounter different errors when send_keys() is being invoked. You have a to address the issues as they show up as per the prevailing condition of the HTML DOM.


References

You can find a couple of relevant discussions on send_keys() in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks, but I don't think it's an issue with waiting. In my case there's embedded javascript, so I need to handle that. – LazyCat Jun 03 '20 at 21:06
  • @LazyCat I didn't speak about waits here in this discussion yet :) However the examples I have provided as reference may include waits as a solution. My answer is pretty generic with respect to _which element to send keys to_ – undetected Selenium Jun 03 '20 at 21:11