Selenium - identify web element using src or alt - python

There is a website with an image button on it which I am trying to click using selenium. The code for this from inspecting the element is:
<img src="../images/aaa.gif" width="100px" height="100px" border="0" alt="aaa">
I know how to use find_element_by_name, id, etc...but am unsure how what to use to click the above. Could anyone advise please?
PS. The image also has a href on a seperate line of code if that makes things simpler?

It the image is always exactly the same, try this xpath:
//img[#src="../images/aaa.gif"]

If the image always has the same ALT attribute you can also use the CSS selector, img[alt='aaa'].

Related

How to get youtube tags using Selenium in Python?

I was able to retrieve all other information that I needed using selenium CSS selector but the video tags. I know I can find the tags if I do the following:
open youtube --> go to a video --> right-click --> view page source --> control + find --> type 'keywords' --> go to the second result.
Does anyone have an idea of how to get a CSS selector or XPATH to get the video tags? Any ideas would be greatly appreciated. Thanks!
Usually you post the code and by analysis we try to adjust what you need. Anyway, to get the XPATH you should locate via HTML the register. Look for the class="style-scope ytd-item-section-renderer" on Youtube. Below that, you might find a list of each video, were the "title" equals the name of the video.
If thats the case, use the CSS from Selenium to locate by your preference!

Clicking on an img with Selenium

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

how to use selenium to click 'checkout' button?

The html is as followed:
enter image description here
I have tried multiple methods like driver.find_element_by_css_selector and others. None worked.please help
Without knowing more information, try to use XPATH and .click()
driver.find_element_by_xpath('//a[#class="button checkout"]').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()

Click a href button with selenium and python?

I have one button from one LinkedIn page with this code:
<div class="primary-action-button"><a class="primary-action label" href="/requestList?displayProposal=&destID=39959446&creationType=DC&authToken=Yr4_&authType=OUT_OF_NETWORK&trk=vsrp_people_res_pri_act&trkInfo=VSRPsearchId%3A2998448551382744275729%2CVSRPtargetId%3A39959446%2CVSRPcmpt%3Aprimary">Send InMail</a></div>
Is there any way to click on an element just by its href link? Thanks
Using selenium you could use the following code:
driver.find_element_by_link_text("Send InMail").click()
The above answer driver.findElement(By.linkText("Send InMail")).click(); is in Java. In python, use find_element_by_link_text:
driver.find_element_by_link_text('Send InMail').click()
or something like this is sometimes helpful
driver.find_element_by_partial_link_text('Send').click()

Categories