Python Webdriver need code to find and click a particular element - python

This is a similar question to one answered for me earlier.
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 and created a board. The problem that I have is to identify and click the “Edit Board” button successfully using xpath find elements code. I attach an image of the web page (see red arrow) and the Chrome inspect on the element. The code I have tried is:
driver.find_element_by_xpath('//span[contains(text(),"Edit board")]')
The result is enabled and displayed, however, click on the element gives the following exception:
WebDriverException: Message: unknown error: Element is not clickable
at point (131, 91). Other element would receive the click: <button
class="Button Module ShowModalButton boardEditButton btn hasIcon
rounded" type="button">...</button>
I cannot see any other find element option which will make the button unique.
I have successfully used the span text find on this website in one other place. It does not work for all buttons of this type, but, in those cases another unique tag qualifier works.
Web Page:
Inspect Element:

Related

RobotFramework - Unable to click element using selenium library but Execute Javascript click works

I am using RobotFramework to automate one application. I am using the selenium library. For the whole application, selenium keywords "Click Button" and "Click Element" throw an error stating "ElementClickInterceptedException: Message: element click intercepted: Element **** is not clickable at point (376, 289). Other element would receive the click: ..."
I am able to identify the element using ID and it is not under any iframe or shadow-root element. but still I am not able to click on the element. I also tried with adding wait commands to see if it is sync issue but it is not. I tried to click using Action class, mouse move and click etc but did not work.
I tried to take the screenshot of the element using "Capture Element Screenshot" and it captures the screenshot of an empty place however, when I try to locate element in the browser dev tool, it locate it exactly
Only working solution I found is to run "Execute Javascript" keyword to click on the element such as
Execute Javascript $('#id').click();
Question:
Though I am able to make it work, I am curious to know what could be the issue in the application. I am not able to share the application dom code due to restriction. Sorry for that
This means that the element you trying to click is
Out of the visible screen (view port) so you need to scroll the page to make that element accessible or
It is covered by some other element - for example you should open a drop down menu etc or
You trying to click the element while it is still not fully rendered - in this case you need to add some delay to make the element fully rendered and be ready to accept clicks.
Selenium generally imitates human GUI actions. So, as a human user you can't click element inside drop-down without opening it. And you can't click element out of the visible screen. This is why Selenium .click() methods can't click such elements.
JavaScript click is more powerful tool, it can click invisible, covered etc. elements. It doesn't imitate human GUI actions.

Selenium ElementClickInterceptedException Error

I am working on this website
Basically I want to capture 2 latest news from News section that is below the table and store that news in some json and collect picture if possible but I am not able to go to the news section as It always raise error saying
selenium.common.exceptions.ElementClickInterceptedException: Message: Element <a id="ctl00_ContentPlaceHolder1_CompanyDetail1_lnkNewsTab" href="#divNews"> is not clickable at point (307,786) because another element <iframe id="webpush-onsite" name="webpush-onsite"> obscures it
here is some part of code I tried tough
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
stocks_data = ["AKPL","NICA"]
for stock_data in stocks_data:
self.driver.get(f'https://merolagani.com/CompanyDetail.aspx?symbol=
{stock_data.lower()}')
self.driver.find_element_by_xpath("//li[#id='navNews']"
"//a[#id='ctl00_ContentPlaceHolder1_CompanyDetail1_lnkNewsTab']").click()
If you really need to click it regardless a normal user won't be able because as the error says, there's an IFrame on top of it...
Try the following:
Set the window size to 1920x1080 or so, maybe the elements overlap because the screen on Selenium's session is too small 🤷‍♂️
Worst case scenario, inject some Javascript code to do it forcefully:
self.driver.execute_script("document.getElementById('ctl00_ContentPlaceHolder1_CompanyDetail1_lnkNewsTab').click()")
Click on exact coordinate (x,y) if you know it ofcs.

Click checkbox with python selenium

so I wanted to click a checkbox on website using selenium (python).That's the button I want to click
So I thought that it would work with that code:
driver.find_element_by_xpath("//input[#name='termsCheck']").click()
But that gives me an errorThat's the error I get
Additional info: there are 2 more checkboxes on the same page which have also <span class="custom-checkbox"> ::before ::after </span>
Has anyone an idea how to get selenium to click the checkbox?
I have seen some scenarios were the element must be clicked with javascript because it is covered by other elements. Alternatively you could click the <span> element that is covering it.
Here is how to click the element with javascript using python and selenium. Since you have not provided the HTML I am assuming that the xpath you provided uniquely identifies the element you want to click.
element_to_click = driver.find_element_by_xpath("//input[#name='termsCheck']")
driver.execute_script("arguments[0].click();", element_to_click )
On most browsers you should be able to copy the XPath or CSS selector by right clicking the specific element on the developer tools console. The click() method should work.
The code is attempting to click the checkbox and Selenium API doesn't like that. The error informs about that, but is not specific enough. Try using auxiliary class Select instead:
from selenium.webdriver.support.ui import Select
element = driver.find_element_by_xpath("//input[#name='termsCheck']")
select = Select(element)
select.select_by_index(index)
Additionally, make sure that XPath //input[#name='termsCheck'] is only matching single element.
Refer to Selenium Python documentation for more details.

ElementClickInterceptedException: element click intercepted: [duplicate]

This question already has answers here:
Element MyElement is not clickable at point (x, y)... Other element would receive the click
(5 answers)
Closed 3 years ago.
I'm trying to submit one form on wordpress plugin using selenium + python. When I press publish button , it is giving an error.
ElementClickInterceptedException: element click intercepted: Element
type="submit" name="publish" id="publish" class="button button-primary button-large" value="Publish"> is not clickable at point (728, 15). Other element would receive the click:
...
I have tried following solutions:
Used action driver, but it didn't work.
Used webdriverwait() function, it didn't work.
Used Xpath, CSS selector, ID - It's giving same error in all three.
` browser.find_element_by_css_selector("""#save-post""").click()
WebDriverWait(driver, 90).until(EC.element_to_be_clickable((By.ID, "save-post"))).click()
`
Note that when I run that specific line in python console, it is working. But while running the full script, it is showing error.
There are so many similar questions on portal but none of them worked. Kindly help me to solve this issue.
Ok, the answer is in error message - "Other element would receive the click: ..."
I had the same problem when I just started using selenium.
Couldn't figure out what's the problem. "Other element would receive the click: ..." means there is other element above(overlapping) your element(pop up window, page is grayed out or disabled while loading, Some JS running etc., so when Selenium trying to click on your element its actually clicking on that blocking element.
Selenium is running really fast and clicking before its become clickable, that's why u are not able to click on it - "when I run that specific line in python console, it is working" Try to click after time.sleep() 5-10 sec or run script with debug mode.
If this is the case then you can use wait or add condition before find your element to check that element that prevent from clicking on you element is not there then u click on your element.

Locate "file upload" button with Selenium

I have the button shown below (image and HTML) and am trying to click it.
Selenium is unable to locate it - I have tried locating by both xpath and by ID.
<input id="wsUpload1" type="file" name="file">
XPATH:
element = driver.find_element_by_xpath('//input[#id="wsUpload1"]')
element.click()
Where am I going wrong?
EDIT: Here is the exception thrown by Selenium:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[#id="wsUpload1"]"}
Possibilities
Duplicate web element with same id in the page.
Element may be in frame. You need to switch to frame
Trying to access the web element before page is loading.Give some wait time.
Not sure why your button wasn't found, maybe it's because of quotes (although it should show you error in that case), try with driver.find_element_by_xpath(".//input[#id='wsUpload1']") and see if it works. I'm not sure is your button already rendered on the page or you trigger it somehow so it's not there yet?
NoSuchElementException is thrown because your targeted element couldn't be found on that page, it could be that you are on the wrong page, element is not rendered yet so you should wait for it to appear, element could be in some iframe etc etc, it's hard to say when I don't know how your page works.
But if you are trying to upload something you should perform sendKeys() on that button (with path of file which are you trying to upload), not click() on it. This is how selenium upload works.
I have solved it - the driver opens a tab on a side panel and the button is in the tab. There seems to be a few ms delay between clicking the tab and the button appearing so I added a wait until element is clickable and that seems to work.
wait.until(EC.element_to_be_clickable((By.XPATH, "//input[#id='wsUpload1']"))).click()

Categories