-1

I am trying to click on an element and send keys to it but i get cannot focus on element error. I tried using action chains but was not working.

I am able to click the element but when i send keys it throws "cannot focus on element error"

D = C.find_element_by_xpath('//*[@id="pcsTableId"]/tbody/tr[9]/td[4]')
>>> D.click()
>>> D.send_keys("4556741")
WebDriverException: Message: unknown error: cannot focus element
(Session info: chrome=59.0.3071.115)

inspect element page

user3443200
  • 89
  • 1
  • 3
  • 8
  • Could you post the html or a link to the site? That would help to determine what the problem is. – Brydenr Aug 01 '17 at 16:11
  • This site has limited access, posting elements on page in details @Brydenr – user3443200 Aug 01 '17 at 16:17
  • which kind of element are you trying to select? it's a button so you click or a field were you can send a key? looks strange that you want click and send a keys to the same element – Carlo 1585 Oct 02 '17 at 10:21

4 Answers4

1

Attempt to replace your click with send_keys(Keys.ENTER) make sure to import Keys: from selenium.webdriver.common.keys import Keys. This solution worked for me on a recent script using Chromedriver.

smb
  • 21
  • 5
1

Please try Actions class to first focus on the element then send required keys.

Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.click();
actions.sendKeys("SOME DATA");
actions.build().perform();

getting cannot focus element in chrome and edge using java/selenium

0

use JavaScript executor to scroll the element into view and then perform click on the element, chrome driver can't click on the element if that element is not shown on the visible screen. i.e. you need to scroll the element up or down in order to make it shown on the screen the click on it.

Please let me know if that works with you.

0

This is a similar problem i ran into, although my solution is in Java you should be able to get the gist of it and translate it to python

private void scrollToElement(WebElement element){
     ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
}

My original post can be found here: WebElement getText() is an empty string in Firefox if element is not physically visible on the screen

Hopefully it helps you out

so cal cheesehead
  • 2,515
  • 4
  • 29
  • 50