How to find and click on HTML image element - python

I'm new with Selenium and struggle with this one for few hours.
I have an HTML page that contains icon and stream view (both images), the browser view is on 100% and I would like to modify it and make it smaller,
it can be done by changing browser zoom, modifying CSS property or clicking on the image.
My code starts with open browser and waits till all elements load:
driver = webdriver.Firefox()
driver.get('http://' + user +':'+password+'#'+camera_address)
driver.maximize_window()
driver.implicitly_wait(15) # seconds
I tried to do so via zoom:
driver.execute_script("document.body.style.zoom='0.4'")
Didn't worked.
Tried to do it via Selenium -- find element and change CSS:
myDynamicElement = driver.find_elements_by_id("stream")
or find the image and click it with:
driver.find_element_by_xpath('//img[#src="../pics/button_downsize_27x27px.gif"]').click()
or
driver.find_elements_by_xpath('//*[#img]').click()
or
driver.find_elements_by_tag_name('img').click()
The HTML page looks like this:
<img src="/pics/button_downsize_27x27px.gif" width="27" height="27" border="0" title="Scale down to 800 px width" alt="Scale down to 800 px width">
<td colspan="3" align="center"><img id="stream" src="/mjpg/video.mjpg" width="2560" height="1920" border="0" alt="If no image is displayed, there might be too many viewers, or the browser configuration may have to be changed. See help for detailed instructions on how to do this."><br></td>
Tried with #title and #alt and even contain but nothing works.
What am I doing wrong?!?! How to find and click on this image (/pics/button_downsize_27x27px.gif)?

To click on your desired image -- please use the reference code mentioned here.
new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfAllElementsLocatedBy((By.cssSelector("a"))));
List<WebElement> elem = driver.findElements(By.tagName("a"));
for(WebElement el : elem){
String element = el.getAttribute("src");
if(element.contains("/pics/button_downsize_27x27px.gif")){
el.click();
break;
}
}
It is in Java, you can implement it in Python.

Thank you all, finally, I did a little workaround since I saw that in chrome there is a little bug and via Firefox, I cannot find the element.
So I retrieve again the URL after all elements loaded and modify it so the JavaScript function will be triggered:
myElem = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.NAME, 'indexMain')))
driver.get(driver.current_url+'&size=8')
Thank you all for trying, this one was a challenge since the element have no id/name/class/css and the browser could not find the XPath.

Related

Python Selenium: Click Instagram next post button

I'm creating an Instagram bot but cannot figure out how to navigate to the next post.
Here is what I tried
#Attempt 1
next_button = driver.find_element_by_class_name('wpO6b ')
next_button.click()
#Attempt 2
_next = driver.find_element_by_class_name('coreSpriteRightPaginationArrow').click()
Neither of two worked and I get a NoSuchElementException or ElementClickInterceptedException . What corrections do I need to make here?
This is the button I'm trying to click(to get to the next post)
I have checked your class name coreSpriteRightPaginationArrow and i couldn't find any element with that exact class name. But I saw the class name partially. So it might help if you try with XPath contains as shown below.
//div[contains(#class,'coreSpriteRight')]
another xpath using class wpO6b. there are 10 elements with same class name so filtered using #aria-label='Next'
//button[#class='wpO6b ']//*[#aria-label='Next']
Try these and let me know if it works.
I have tried below code and it's clicking next button for 10 times
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
if __name__ == '__main__':
driver = webdriver.Chrome('/Users/yosuvaarulanthu/node_modules/chromedriver/lib/chromedriver/chromedriver') # Optional argument, if not specified will search path.
driver.maximize_window()
driver.implicitly_wait(15)
driver.get("https://www.instagram.com/instagram/");
time.sleep(2)
driver.find_element(By.XPATH,"//button[text()='Accept All']").click();
time.sleep(2)
#driver.find_element(By.XPATH,"//button[text()='Log in']").click();
driver.find_element(By.NAME,"username").send_keys('username')
driver.find_element(By.NAME,"password").send_keys('password')
driver.find_element(By.XPATH,"//div[text()='Log In']").click();
driver.find_element(By.XPATH,"//button[text()='Not now']").click();
driver.find_element(By.XPATH,"//button[text()='Not Now']").click();
#it open Instagram page and clicks 1st post and then it will click next post button for the specified range
driver.get("https://www.instagram.com/instagram/");
driver.find_element(By.XPATH,"//div[#class='v1Nh3 kIKUG _bz0w']").click();
for page in range(1,10):
driver.find_element(By.XPATH,"//button[#class='wpO6b ']//*[#aria-label='Next']" ).click();
time.sleep(2)
driver.quit()
As you can see, the next post right arrow button element locator is changing between the first post to other posts next page button.
In case of the first post you should use this locator:
//div[contains(#class,'coreSpriteRight')]
While for all the other posts you should use this locator
//a[contains(#class,'coreSpriteRight')]
The second element //a[contains(#class,'coreSpriteRight')] will also present on the first post page as well, however this element is not clickable there, it is enabled and can be clicked on non-first pages only.
As you can see on the picture below, the wp06b button is inside a lot of divs, in that case you might need to give Selenium that same path of divs to be able to access the button or give it a XPath.
It's not the most optimized but should work fine.
driver.find_element(By.XPATH("(.//*[normalize-space(text()) and normalize-space(.)='© 2022 Instagram from Meta'])[1]/following::*[name()='svg'][2]")).click()
Note that the XPath leads to a svg, so basically we are clicking on the svg element itself, not in the button.

Refreshing the source code without refreshing the web page

I want to measure the number of cookies regarding of the policie accepted by the user.
So, for example, one the website https://sephora.fr i'm first accessing to the control panel :
driver = webdriver.Chrome(PATH)
driver.implicitly_wait(10)
driver.get(website)
driver.find_element_by_id('footer_tc_privacy_button').click()
Then i would like to click on the black button ("Enregistrer")
driver.find_element_by_id('save-consent').click()
The problem is that the HTML code source is updated after the first click and selenium keep the initial source code -> selenium can't find the button 'save-content'.
Unfortunately i can't refresh the page because it will close the control panel.
I tried to sleep some seconds after the first click, but it's not working.
Any idea ?
Edit : i also tried to switch to the Iframe
frame = driver.find_element_by_xpath('//frame[#name="privacy-iframe"]')
driver.switch_to.frame(frame)
because the button Enregistrer is on this iframe
<iframe id="privacy-iframe" class="tc-reset-css tc-privacy-center-iframe" src="https://cdn.trustcommander.net/privacy-center/default/modern/index.html" title="Vos paramètres cookies" lang="fr"></iframe>
but it's not also not working
EDIT : Solution
I switched to the iframe
frame = driver.find_element_by_id('privacy-iframe')
driver.switch_to.frame(frame)
driver.find_element_by_id('save-consent').click()
then i switched back to the parent
driver._switch_to.parent_frame()
I do not see the name in the iframe tag attribute shared by OP :
try with ID instead :
frame = driver.find_element_by_id('privacy-iframe')
driver.switch_to.frame(frame)

Having a hard time clicking on a radio button that activates a drop down. Using Selenium and Python

SO normally I don't have a hard time with radio buttons but I was trying to configure the web browsers set home page to custom URL from the chrome settings. I knew I would have to activate the drop down box that lets me (picture provided). I located what I think is the source and had to navigate my way through some shadow DOM's. however after getting to the path I tried to click on it I get the error
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (311, 1418)
I'm confused and I've been fighting this off for a while. Any one have any idea? I did notice some of the settings change when I personally click on the different options. here are the pictures
Heres my code:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
def expand_shadow_element(element):
shadow_root = cdriver.execute_script('return arguments[0].shadowRoot', element)
return shadow_root
#chrom driver
cdriver = webdriver.Chrome(executable_path='C:\\Users\\name\Downloads\\chromedriver.exe')
#open up page to chrome settings.
cdriver.get('chrome://settings/')
root1 = cdriver.find_element_by_tag_name('settings-ui')
shadow_root1 = expand_shadow_element(root1)
root2 = shadow_root1.find_element_by_id('main')
shadow_root2 = expand_shadow_element(root2)
root3 = shadow_root2.find_element_by_tag_name('settings-basic-page')
shadow_root3 = expand_shadow_element(root3)
root4 = shadow_root3.find_element_by_tag_name('settings-on-startup-page')
shadow_root4 = expand_shadow_element(root4)
root5 = shadow_root4.find_element_by_name('4')
shadow_root5 = expand_shadow_element(root5)
root6 = shadow_root5.find_element_by_id('button')
root6.click()
anyone have any idea why I cant click on the source? i even right clicked on the radio button and thats the source I was pointed to.
To work around the element click intercepted error, you can try a Javascript click and see if that works for you.
The DOM tree is a bit hard to follow, but I think I catch the drift here -- a radio setting is under controlled-radio-button element, and the little circle itself is <div class='disc'>, as highlighted in your code sample.
I didn't see a shadow root under <div id="button"> that you store in root6 -- I see the element, but not a shadow root, so I am assuming the radio button itself actually lives under root5.
With that being said, here's some code to click on a radio button (given its description) using Javascript:
# grab the radio element... tricky with shadow roots
radio_button = root5.find_element_by_xpath("//div[#id='button']/div[#class='disc']")
# attempt to click it using JS -- ignores ClickIntercepted error
driver.execute_script("arguments[0].click();", radio_button)
Because you have already located the correct radio group and stored the shadow elements in root5 variable, you can try to use find_element_by_xpath on root5 to grab the <div class="disc"> element that appears right under it. The Javascript click should hopefully work around the ClickIntercepted error.

Can't Click an Object in Selenium in Some Google Image Search Results

I'm trying to pull urls of images from Google Image Search results. As you know from using Google Image Search you click on a small photo in the grid to get a bigger image to pop down below it. This is where my Selenium Chrome Webdriver stops.
I've tried the .click() command and the .send_keys(Keys.RETURN) command. I've also tried the .send_keys(Keys.CONTROL + Keys.RETURN) command to try to open it in a new tab.
There are four different elements under the search result of the small image I want to click on in order to get at the full image. Three of the elements return a
"selenium.common.exceptions.WebDriverException: Message: unknown error: cannot focus element"
The X-Path in the code below doesn't give that error but it doesn't do anything. Terminal just goes back to the $ prompt.
I can't seem to get the photo and buttons below to pop down for anything. When I tell Selenium to do things to elements in the menu I can't get to open it says the element is not visible.
I don't want to use their API because I need more images than their API limits.
Here's the code. I'm thinking of another option below this code.
import requests
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
list3 = ["rags and scraps"]
#Setup The Automated Browser (brew install chromedriver)
options = webdriver.ChromeOptions()
options.add_argument('user-data-dir=/user/anthonycelio/selgoog/chrome_driver') #Path to your chrome profile
driver = webdriver.Chrome('/usr/local/Cellar/chromedriver/2.27/bin/chromedriver')#, chrome_options=options) #Type 'brew list chromedriver' to find path
driver.get("https://www.google.com/search?q=cats+and+dogs&safe=off&espv=2&biw=1599&bih=726&source=lnms&tbm=isch&sa=X&ved=0ahUKEwiI56_7isXSAhXC4iYKHcbZCLEQ_AUIBigB#q=cats+and+dogs&safe=off&tbm=isch&tbs=sur:fc&*")
for i in list3:
driver.find_element_by_xpath("""//*[#id="lst-ib"]""").clear()
inputElement = driver.find_element_by_xpath("""//*[#id="lst-ib"]""")
inputElement.send_keys(list3)
driver.find_element_by_xpath("""//*[#id="_fZl"]""").send_keys(Keys.RETURN)
driver.find_element_by_xpath("""//*[#id="rg_s"]/div[1]/a""").click()
This might be a different option to store the url of the image if Google has made their images unclickable by Selenium: One of the elements has the link in it if you copy the outerHTML. The problem is they have stuffed code into the link. I wonder if it can be removed with any consistency.
<img class="rg_ic rg_i" data-sz="f" name="ziffAZE9s2oycM:" alt="Image result for rags and scraps" jsaction="load:str.tbn" onload="google.aft&&google.aft(this)" src="data:image/j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rskRcdyA2ORF90IYBuQubfQqDnkAIqBnvCaqCQRJCeoiBoqh2m4zdOPEjUpkRKcc61oVQuUGxhLSEW+SBsGEL6ehlBIfrIRuuE0O8ImGUCioYUerULd1k5VEXmyGJUVxfOlj3Is4IgqMTBiUtVh1CLgxa25E1saeSbZCVj4MHVQESDcG6g42mImO1+qk1WkXATWLfNM+HxCs6iGCuaYXLl2yMFNQoQVy5TIulNQouvP2Eq5MhoDUKU13cVy5XIa4Vz9hL15XLlMhtJ15ghL+0OiFy5PMNrhiHLhiXcvJKuTIaQ4l3LyXftLuPuSrkyGkOJdxXftLuK5cmQ1wxTuPuXOxbjw8guXJkNIzEuGh9yU4t3H3JVyZAgxj+KX9sfx9wXLkyGkdi3HU+4IG1iLSuXJkNK6sTvRNxDuPuXLkyG137Q7ihfXcdSuXJkNohiXj8ybfVJ1K5crkNf/Z" style="width: 209px; height: 161px; margin-left: -10px; margin-right: -11px; margin-top: 0px;"><div class="_aOd rg_ilm"><div class="rg_ilmbg"><span class="rg_ilmn"> 7290 × 5614 - commons.wikimedia.org </span></div></div>
https://upload.wikimedia.org/wikipedia/commons/a/ab/Frank_G._Carpenter,_Salvaging_scraps_on_the_streets_of_Paris.jpg
The easiest option would be the second thing you mentioned. You can extract the href value, then run it through urllib's unquote function. When you are on the image results page:
import urllib
images = driver.find_elements_by_class_name('rg_l')
for image in images:
href = image.get_attribute('href')
if '?imgurl=' in href: # Do this because often times the last result will not be an actual image
image_url_raw = href.split('?imgurl=')[1].split('&imgrefurl=')[0]
image_url = urllib.unquote(urllib.unquote(image_url_raw))
If you are on Python3, you need to use urllib.parse.unquote(urllib.parse.unquote(image_url_raw)) instead

Selenium Hover/Click event on ajax filled menu options

Having serious issues here. Someone please help.
I am trying to login to a website. - This Works!
Redirect to the page I want after Login - This Works!
Now once in, I have to hover over the settings icon so the dropdown shows, and then click on the "Settings" options that has NO ID or CLASS or HREF.
Now there is a couple of reasons I cant do this. Number 1 is if I try to click on the link after the hover, it tells me that it's hidden and I cant interact with it. Also the menu options in the form are populated and appended once you hover, through ajax I think. They are not on the initial page load.
wait = WebDriverWait(driver, 10)
box = wait.until(EC.visibility_of_element_located((By.ID, "yucs-help_button")))
menuButton = driver.find_element_by_id("yucs-help_button")
ActionChains(driver).move_to_element(menuButton).perform()
After the above code is deployed I print the driver.page_source and can see (below) that the new menu options are there, if you don't hover, the below code will not be on the page.
Now the <a> i'm trying to click is the <span>Settings</span> option, and for the life of me, it will not work. Either can't find it, not clickable, can't interact with it, etc, etc, etc. xpath, css_selector, partial_name, nothing finds this thing. Also whats weird is once you click on it, from a browser, it appends an ID to <span> So weird. Any ideas?
<a data-ylk="rspns:nav;t3:tl-lst;t5:custitm;slk:custitm;elm:itm;elmt:custitm;itc:0;cpos:2" class="C(#000)! Td(u):h " data-mad="options" data-customevt="true" href="#" data-rapid_p="18"><span>Settings</span></a>
To perform mouse over event on element you should try to use .execute_script() using following java script :-
wait = WebDriverWait(driver, 10)
box = wait.until(EC.visibility_of_element_located((By.ID, "yucs-help_button")))
menuButton = driver.find_element_by_id("yucs-help_button")
driver.execute_script("var clickEvent = document.createEvent('MouseEvents');clickEvent.initEvent('mouseover', true, true); arguments[0].dispatchEvent(clickEvent);", menuButton)
Now after successfully mouse over you should try to click on Settings link as below :-
driver.find_element_by_xpath("//span[contains(text(), 'Settings')]/parent::a[#data-mad = 'options']").click()
Hope it will help you..:)

Categories