Cannot focus element using selenium - python

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

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.

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

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.

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

Related

I have a selenium python issue on select an html element icon and click

I'm trying to click to close a message from a website with selenium. However, when I put it to click, a message appears in the Visual Studio Code console saying that it was not possible to click on the element because it is not a clickable element.
sleep(5)
web.find_element(By.XPATH, '//*[#id="top-container"]/div[1]/div/i').click()
devtool element
error https://i.stack.imgur.com/sgzoE.png
if anyone knows any library that delete the element in devtool. why do i need to remove that message to appear another button to proceed with application
If you look at the error message carefully, it doesn't state that it's not a clickable element. It states that the click was intercepted. In other words, Selenium tried to click on the X to close but another element, an <h3>, got in the way.
It looks like your locator is fine. According to the error message, it looks like it's finding the right element. I personally would change it to
web.find_element(By.CSS_SELECTOR, 'i.icon-remove').click()
because I think it's more readable and less likely to click the wrong element.
I can't see the page so I have no idea what h3 is getting in the way and if it's possible to even remove it. So, if you can't get around the h3, you will likely have to use JS to click the element.
icon = web.find_element(By.CSS_SELECTOR, 'i.icon-remove')
driver.execute_script("arguments[0].click();", icon)
There are different ways to call an element. Let's try the following:
web.find_element(By.ID, "top-container").click()
Please let me know if works, either way we can see other options

Why won't Selenium in Python click the pop-up privacy button?

I am having some issues with Selenium not clicking the pop-up privacy button on https://www.transfermarkt.com/
Here is my code so far:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.transfermarkt.com/')
accept_button = driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div[2]/button')
accept_button.click()
It comes up saying:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div[2]/div[3]/div[2]/button"}
Anyone have any thoughts?
I believe the issue is that the element you are trying to click is inside an iframe. In order to click that element you'll have to first switch to that frame. I noticed the iframe has title="SP Consent Message" so I'll use a CSS Selector to identify it based on that. Your code with the added line:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.transfermarkt.com/')
driver.switch_to.frame(driver.find_element_by_css_selector('iframe[title="SP Consent Message"]'))
accept_button = driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div[2]/button')
accept_button.click()
Note that you may have to switch back to the default frame to continue your test, I'm not 100% sure as that iframe has gone away.
Also it seems like some folks don't get that popup when the hit the website, not sure why, may be something you want to look in to to determine how you want to test this.
Like #Prophet says you should improve the xpath for the button (again it seems to have a unique title so I would use CSS Selector 'button[title="ACCEPT ALL"]'), but this works for me to click it.
There are 2 issues here:
You need to add wait / delay before accept_button = driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div[2]/button') to let the page load
It's very, very bad practice to use absolute XPaths. You have to define an unique, short and clear related XPath expression.
Additionally, I see no pop-up privacy button when I open that page. So, maybe that element is indeed not there.

Problems with typing text using splinter

I want to type text, and send it like showed in the following link
example of form to be filled out.
I am however unable to type anything in the field. I supposed at first it was an iframe, but splinter is unable to locate it. If i search by tags for the body, it says that:
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot focus element
How might i go about fixing this?
if your element is not focused you have to first move to that element using selenium actions as following:
first move to using ;
driver.switch_to.frame(driver.find_element_by_css_selector("#inquiry-content_ifr"))
Then focus on element
yourElement= driver.find_element_by_css_selector(".nav")
actions = ActionChains(driver)
actions.move_to_element(menu)
actions.perform()
then do your interaction with that element after focus has been set to the element.

Python Webdriver need code to find a particular element

I have used Python 2.7, Webdriver and Chrome to access Pinterest to insert images to a board. I have successfully logged in to the site, created a board and clicked on the Pin Image button (thanks to Stack Overflow). The problem that I have is to identify and click the “No Thanks” button using xpath find elements code. I attach an image of the web page and the Chrome inspect on the element.
Pinterest 'Popup'
Not Now Element code
I guess you can give a try to this xpath, which will grap the first element containing the class "cancelButton". Hopefully, the button on your popup will be the first element on the page containing this class.
button[contains(#class, 'cancelButton')]
hope this helps
//span[contains(text(),'Not now')]
this is general syntax: //tag[contains(attribute,‘value’)]

How to take elements by Selenium which created by JS script

I trying automating testing with Selenium (python bindings), specifically want to log in on tokmonet.zendesk.com.
I create a script which takes email field, password field and sign in button by id.
But when I ran script it fails with
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"id","selector":"user_email"}
Inspecting page with Firebug I see these elements. But when trying to get them with Firefinder it couldn't.
So, I perform
html_source = driver.page_source
print(html_source)
and get the only
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>
When I check page source it contains only js scripts and no mark up.
Please advice how I could handle this elements?
I see that elements that you are trying to log in are in an iframe in tokmonet.zendesk.com and so you are not able to get the elements. To handle such situation try to switch to the iframe first and then get the elements. Here's how to do it in Java -
driver.switchTo().frame(driver.findElement(By.tagName("iframe")));
(new WebDriverWait(driver, 20))
.until(ExpectedConditions.presenceOfElementLocated(By.id("user_email"))).sendKeys("username");
//Similarly you can get other elements too
You similarly implement it in other languages. Hope this helps.
You need to switch to the IFRAME, then send_keys() to the element which you can find by ID. Don't forget to switch back to the default content if you need to access elements outside the IFRAME.
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
driver.find_element_by_id("user_email").send_keys("username")
driver.find_element_by_id("user_password").send_keys("password")
// do whatever else
driver.switch_to.default_content()

Categories

Resources