Clicking on an img with Selenium - python

I'm pretty new to coding in Python and in general.
So far, I've tried:
driver.find_element_by_xpath("//img[contains(#src,'/images/excel.png')])
driver.find_element_by_css_selector("/images.excel.png")
driver.execute_script("javascript: exportExcel('')")
driver.find_element_by_xpath("//td[#class='pageControl'][img/#src='/images/excel.png']").click()
Any help would be appreciated, thank you in advance.
Update:
I've attached an additional image of the html code that is above the code depicted in the DOM in the previous image. There does not seem to be an iFrame obstructing the img
Update 2: I used pyautogui to physically move the mouse to a specified coordinate to click the icon on the page, as an alternative solution. So far, xpaths have failed to identify the element.

I would tackle this by clicking on the parent td element. My understanding of your question is that you are trying to get the excelExport() function to trigger.
Using a CSS Selector:
td_element = driver.find_element_by_css_selector('td[onclick="javascript: excelExport()"]')
td_element.click()
The image itself could be selected with:
image = driver.find_element_by_css_selector('img[src="/images/excel.png"]')
Not tested (but it might work):
driver.execute_script("excelExport();")
When running driver.execute_script, the language does not need to be specified, as it will be javascript.

I haven't seen you attempt a Javascript click in your solutions yet, so let's try that. This is usually my catch-all for clicking on weird or funky elements.
image = driver.find_element_by_xpath("//img[contains(#src,'/images/excel.png')]")
driver.execute_script("arguments[0].click();", image)
Hope this helps a bit.

Click using the Action class:
element = driver.find_element_by_xpath("//table//tbody//tr//td[contains(#class,'pageControl') and contains(., 'Excel')]//img[contains(#src,'/images/excel.png')]")
action = ActionChains(driver)
action.move_to_element(element).click(element).perform()
Following import:
from selenium.webdriver import ActionChains

Related

Check AngularJS checkbox with Selenium

HTML Source here
It seems trivial, but after hours of attempts I cannot manage to check this checkbox using Selenium. I am able to select the element no problem, and have even been able to 'highlight' (see code snippet below) it by sending Keys.RETURN to the element, but on attempting to click it, nothing happens. Perhaps someone has an idea?
What I have tried already:
Using with WebDriverWait
Using multiple and different combinations of .click()/.send_keys(Keys.RETURN)
Just about every combination of XPATH/css selector/id/name/classname/text that Selenium would accept for the element, and all of its children and most of its parents (including spans/labels, and the text inside the label itself).
Directly clicking the element/label coordinates using actions (nothing happens, even when I can 100% confirm it is clicking the right spot by using context_click()).
Using execute_script to change the "checked" attribute to True (the checkbox appears checked, but it is clear it is only client side as a box that is supposed to render when it is actually clicked doesn't).
Using execute_script to change the class to 'ng-valid ng-not-empty ng-dirty ng-valid-parse ng-touched'
Here is the code that I feel got me the closest (it is 'highlighting' the box as seen here)
browser.find_element(By.ID, "sp_formfield_none_of_the_above").send_keys(Keys.RETURN)
Managed to solve it after another while of testing. Hopefully this helps someone in the future. I had to use JS to execute .click() on the checkbox element. In retrospect, I should have tried this solution sooner. See code snippet:
cb_none = browser.find_element(By.ID, 'sp_formfield_none_of_the_above')
browser.execute_script("arguments[0].click();", cb_none)

Issue selecting button by xpath, Python, tinder

I struggle to select the Tinder like button. I just copied the Xpath from the web version of tinder. It works for the Dislike button not for the like button. I've also tried to add a sleep button in case the like button didnt render quickly enough.
def dislike(self):
dislike_btn = self.driver.find_element_by_xpath('//*[#id="content"]/div/div[1]/div/main/div[1]/div/div/div[1]/div/div[2]/button[1]')
dislike_btn.click()
def like(self):
like_btn = self.driver.find_element_by_xpath('//*[#id="content"]/div/div[1]/div/div/main/div/div[1]/div/div[2]/button[3]')
like_btn.click()
I've tried to select by CSS Selector but that didnt work either.
I am an absolute noob, so have mercy if I missed out on some relevant infos :D
try clicking the element directly without assigning it. This is working for me and also check if there are any overlapping elements on the like button.
driver.find_element_by_xpath('//*[#id="content"]/div/div[1]/div/main/div[1]/div/div/div[1]/div/div[2]/button[3]').click()

How to click on an element based on its coordinates with selenium webdriver

So, the web app we're developing has a TV/PC mode, and i'm testing the ability to transit between these two modes. `
def pc_to_tv(self):
pc_to_tv = self.driver.find_element_by_xpath(
'html/body/div[1]/div/topbar/header/div[2]/div[2]/div[1]/button[1]')
pc_to_tv.click()
def tv_to_pc(self):
tv_to_pc = self.driver.find_element_by_xpath(
'html/body/div[1]/div/topbar/header/div[2]/div[2]/div[1]/button[2]')
tv_to_pc.click()`
The problem is, when i switch from pc to tv, the screen "zooms in", making the button appear in the same place it would be without the zoom. so, i can't click on the button with my 'tv_to_pc' method, 'cause instead on clicking on the actual button, it clicks where the button should be.
So, the solution i found was clicking on the button with coordinates, that way i'll actually click on the place i want, instead of clicking on an unclickable place like i was doing.
The thing is, i don't know how to do this, and need help on this matter.
I would suggest that you just click the button using JavaScriptExecutor. It will click it no matter where it is on the page. See How to execute a javascript in a Python webdriver and other questions for more info. The general format is
element = driver.find_element_by_id("someId")
driver.execute_script("arguments[0].click();", element)
Also... you don't want to use XPaths like that. Any XPath that starts at the HTML tag or is more than just a few levels deep is going to be very brittle. Do some googling on selenium xpaths and read some guides for more info.
try moveToElement and then perform click
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
new Actions(driver).moveToElement(element, x, y).click().perform();
x is xoffset
y is yoffset
Please see that if you use Javascript for click its not going to be native click.

Selenium and Javascript

I'm trying to learn how to use selenium. I'm trying to work on creating script to like instagram photos; however, i'm running into a problem where xpath won't detect the image i want to click. I think this is probably due to the fact it's a javascript button.
This is a picture of the element i am inspecting. There's multiple pictures on the site and i am given the line
<a class="thumb-shadow" href="javascript:void(0);"></a>
https://gyazo.com/558df373e6ac426f098759665fd8f918
I've tried clicking the xpath of image wrapper, but it doesn't work either. How can i click the javascript item? Are there any resources you can point me to?
Thanks
Try driver.find_element_by_xpath("//a[#class='thumb-shadow']/img").c‌​lick()

Python Chrome Selenium clicking on hidden icon

Using python selenium (chromedriver) I am trying to click on a section of an element and can't seem to figure out how to do this. In the image below, how would I click on the "gear" icon?
As per provided image, i am expecting that, that gear image will be displayed as soon as moving mouse to Time. So first move mouse then click there..
in Java
Actions move=new Actions(driver);
move.moveToElement(driver.findElement(By.xpath("pathToTime"))).click(driver.findElement(By.xpath("pathToGear"))).build().perform();
Thank You,
Murali
First get the element using method 'find_element_by_xpath' and then perform click operation on the returned object. Below is the sample code that will help you.
CODE
import time
...
elem = driver.find_element_by_xpath('//xpath/to/your/button/gear')
elem.click()
time.sleep(5) # for development purposes to make sure that click has successfully performed
The above script will perform a click on the button/tag.

Categories