want to find link/text and Click on it - python

I am using fake mail generator tool to send a mail and click on a link in the mail..
enter link description here
<iframe id="emailFrame" frameborder="0" width="100%" height="360px" src="http://www.fakemailgenerator.com/email/dayrep.com/testlk/message-108204614/" scrolling="no" onload="autoResize('emailFrame')">
<html>
<head>
<body>
<div>
<p>Good day - </p>
<p>You have been assigned an Action from the motion A Name iwhirxpppk: s.
</p>
<p>Kindly follow - up on the Touchpoint Action listed below.
</p>
<ul>
Please click the below link to complete your Action.
<p>
<a target="_blank" href="http://cfn- svr001.cloudapp.net:7100/Home/ActionResponse?eid=ygfWFB5a99mtAUQBxjNUDHjpC9AdFz/9&tpid=14iwlvior8ak6FGifOI3MSBNxnNvHiT9">Click here
</a>
</p>
This email has been generated from CFN Insight by Auto man, auto#
</div>
</body>
</html>
</iframe>
want to find below part of the code
<p>
<a target="_blank" href="http://cfn-svr001.cloudapp.net:7100/Home/ActionResponse?eid=ygfWFB5a99mtAUQBxjNUDHjpC9AdFz/9&tpid=14iwlvior8ak6FGifOI3MSBNxnNvHiT9">Click here</a>
</p>
I tried all possible combinations what ever I know but nothing helped me..
Here are the scripts which I tried
browser = webdriver.Firefox() # for b in
browser.find_element_by_id('emailFrame').find_elements_by_xp‌​ath(".//*"):
print b # browser.find_element_by_xpath("html/body/div[1]/p[4]/a") #
browser.find_element_by_xpath(".//*[text()='Click here']") #
browser.find_element_by_xpath[".//a[contains(., 'Click here')]"] #
browser.find_element_by_xpath(".//div[1]//p[4]/a")
browser.find_element_by_id('emailFrame').find_elements_by_ta‌​g_name("a"):

First, you should switch to the frame and then find the element inside it.
try as follows:
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Firefox()
browser.maximize_window()
browser.get("http://www.fakemailgenerator.com/inbox/armyspy.com/morly1985/")
frame = browser.find_element(By.ID, 'emailFrame')
browser.switch_to_frame(frame) # switch to frame by finding the iframe webelement
click_here = browser.find_element_by_xpath(".//*[text()='Click here']") # try different xpaths.
click_here.location_once_scrolled_into_view # As the element needs scroll down
click_here.click()
References:
https://stackoverflow.com/a/40759300/2575259
http://selenium-python.readthedocs.io/navigating.html#moving-between-windows-and-frames
http://selenium-python.readthedocs.io/api.html

Related

I want to show the **href** context 1,2,3,4,5,6,7,8,9 via python selenium library

I want to show the href context 1,2,3,4,5,6,7,8,9 via python selenium library, but the following coding cannot click the link, please help !
URL link:
https://tmpfiles.org/dl/63714/example_2021.html
HTML code
<html>
<body>
<div class="GGGGG">
<div class="AAAAA">
<i class="FFFFF FFFFF"></i>
</div>
<div class="AAAAA">
<span class="IIIII" id="HHHHH">11-11-1111</span>
</div>
<div class="AAAAA">
<i class="EEEEE EEEEE"></i>
</div>
<div id="AAAAA" class="AAAAA"><div>
1</div> <div>
2</div> <div>
3</div> <div>
4</div> <div>
5</div> <div>
6</div> <div>
7</div> <div>
8</div> <div>
9</div> <div>
10</div> </div>
</div>
</body>
</html>
My Python code
driver.find_elements_by_class_name("AAAAA")[0].click()
I can list out by following code:
c = driver.find_elements_by_class_name("AAAAA")
d = []
for x in range (len(c)):
d.append(c[x].text)
print(d)
result of this coding:
[]
Error Message from the code:
ElementNotInteractableException: Message: Element <div class="AAAAA"> could not be scrolled into view
Expected Result
[1,2,3,4,5,6,7,8,9,10]
I think the following should work here:
links = driver.find_elements_by_xpath("//div[#id='AAAAA' and #class='AAAAA']//a")
for link in links:
link.click()
You will obviously have to put some delay / wait before this code block to ensure the page is fully loaded before you getting all these elements.
UPD
If you need to print these links texts you can do this:
links = driver.find_elements_by_xpath("//div[#id='AAAAA' and #class='AAAAA']//a")
for link in links:
print(link.text)
If it's in the same page then I guess it must scroll down till certain point to display content :
action = ActionChains(driver)
c = driver.find_elements_by_class_name("rno")
for ele in c:
action.move_to_element(ele).click().perform()
Imports :
from selenium.webdriver.common.action_chains import ActionChains
in case you need bit more scrolling, you can try the below solution :
for ele in c:
action.move_to_element(ele).click().perform()
sleep(2)
driver.execute_script("window.scrollTo(0, 100)")
udpate 1 :
c = driver.find_elements_by_css_selector("div.AAAAA a")
d = []
for x in range (len(c)):
d.append(x.text)
print(d)

Having trouble finding/clicking specific images within a div class through selenium

Practicing web scraping through selenium by opening user's dating profiles through a dating site. I need selenium to save a href link for every profile on the page but not sure how to go about selecting it since each link is different for every profile and image. All of the profiles start with the same two div class/style which is "member-thumbnail" and "position: absolute". Thank you for any help that you can offer.
<div class="member-thumbnail">
<div style="position: absolute;">
<a href="/Member/Details/LvL-Up">
<img src="//storage.com/imgcdn/m/t/502b24cb-3f75-49a1-a61a-ae80e18d86a0" class="presenceLine online">
</a>
</div>
</div>
Try using more general selector as follow
.member-thumbnail a
photo = browser.find_element_by_css_selector('.SELECTOR #GOES HERE').click()
So it should look something like that
photo = browser.find_element_by_css_selector('.member-thumbnail a').click()

Get link when clicked in an image

This is the image where, when clicked, user is redirected to another page.
<div class="lis_el " id="cel_lisimg_18755" onclick="lis_mostrarficha(0);">
<div class="lis_elc ">
<div class="lis_eloverflow">
<div class="lis_elc_img">
<div class="lis_elc_imgc"><img class="lis_elc_img_img" id="lisimg_18755" src="https://sgfm.elcorteingles.es/SGFM/dctm/MEDIA03/201705/29/0280282401564764342_1_.jpg">
</div>
</div>
</div>
<div class="lis_info ">
<div class="clear"></div>
<div class="lis_info_precio">
5<span class="lis_info_preciop">,99€</span>
</div>
<h2>Camiseta flame</h2>
<div class="lis_mascol displaynone" id="lis_mascol18755" style="display: block;">+ Colores</div>
</div>
</div>
</div>
I'm tryin to obtain that link using Selenium in Python, but I don't know where I can obtain it from. I noticed this however, which I suppose this function does the redirection:
onclick="lis_mostrarficha(0);
I don't have much experience in web developing so I'm not sure how I can obtain that link without clicking, as this would take too long.
Thanks,
You will have to perform the click event in this case because the HTML does not contain the URL linked to the image -- it calls a script. What can be done is to use Selenium to click the element that contains the onclick event.
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
options = Options()
options.add_argument('--disable-infobars')
driver = webdriver.Chrome(chrome_options=options)
div = find_element_by_id('cel_lisimg_18755')
div.click()
# Then wait for the page to load
# Get the URL
url = driver.current_url
print(url) # Assumes v3 python

div tag not populating, using selenium python, inner HTML

I'm trying to access what appears to be a hidden table within a div tag on the following page:
whoscored.com
...under the link "Passing"
from selenium import webdriver
driver = webdriver.Chrome()
base_url = "https://www.whoscored.com/Matches/959574/LiveStatistics/England-Premier-League-2015-2016-West-Bromwich-Albion-Stoke"
driver.get(base_url)
First i click the link:
elem = driver.find_element_by_link_text("Passing")
elem.click()
driver.implicitly_wait(10)
Next, I try to get the innerhtml of the tag where it appears this table resides.
demo_div = driver.find_element_by_id("live-player-home-passing")
print demo_div.get_attribute('innerHTML')
print driver.execute_script("return arguments[0].innerHTML", demo_div)
But the innerhtml comes up empty in that tag. Very frustrating, because I see the data on the page, but can't figure out a way to grab it.
Any ideas? I would greatly appreciate any help.
Edit: Here is the HTML:
<div id="live-player-home-passing" class="statistics-table-tab">
<div id="statistics-table-home-passing-loading"></div>
<div id="statistics-table-home-passing"></div>
<div id="statistics-table-home-passing-column-legend"></div>
</div>
The data is within 3rd tag, but only visible when I do "Inspect Element":
<div id="live-player-home-passing" class="statistics-table-tab" style="display: block;">
<div id="statistics-table-home-passing" data-fwsc="1">
<table id="top-player-stats-summary-grid" class="grid with-centered-columns hover">
<thead id="player-table-statistics-head">
.....
</thead>
</table>
</div>
The source its what cames from the server and the inspect its your browser representation of the information
try to get the innerHTML directly from the webelement and not js script
table = driver.find_element_by_id("top-player-stats-summary-grid");

How to click/submit onclick values in Python

I have a question about the Selenium Python code I'm writing.
The website has a bunch of time slots, and I'm trying to simulate a mouse click, specifically targeting the 8:30-11:30 timeslot.
Here is a snippet the html:
<div class="item-link" onclick="$('#SelectedStartTime').val('2015/10/16 08:30:00');$('#frmTimes').submit();">
<div class="item">
<div class="title">8:30 AM-11:30 AM
<span class="available">(Spaces: <strong class="num">20</strong>)</span>
</div>
</div>
</div>
<div class="item-link" onclick="$('#SelectedStartTime').val('2015/10/16 09:00:00');$('#frmTimes').submit();">
<div class="item">
<div class="title">9:00 AM-12:00 PM
<span class="available">(Spaces: <strong class="num">20</strong>)</span>
</div>
</div>
</div>
</body>
</html>
I've tried targeting the "8:30 AM-11:30 AM" text,
Time0830 = driver.find_elements_by_class_name("title")
for each in Time0830:
if "8:30 AM-11:30 AM" in each.text:
each.click()
break
for each in Time0830:
if each.text == "8:30 AM-11:30 AM"
each.click()
break
Both do not do anything.
I've also tried targeting the onclick based on this question.
Time0830 = driver.find_element_by_xpath('.//div[contains(#onclick,"8:30 AM-11:30 AM")]')
print (Time0830)
Time0830.click()
This resulted in Error: NoSuchElementException
Time0830 = driver.find_element_by_css_selector("div[onclick*='8:30 AM-11:30 AM']")
print(Time0830)
Time0830.click()
This also resulted in Error: NoSuchElementException.
What do I need to do to get this to work?
EDIT!!
Thanks for the help on xpath help. I think the problem was also that I could not get it to click.
I've been using .click(), but it's not working any more.
My code is now:
time.sleep(1.5)
Time0830 = driver.find_element_by_xpath('//div[#class = "item-link" and .//div[#class = "title" and contains(text(),"8:30 PM-11:30 PM")]]').click()
But the page does not move, still. I have time.sleep to account for loading time.
I've tried it without the click() and it seems to find an element. Am I finding the wrong element, or using the wrong function?
How about you would match a div with item-link class that has a descendant div element having 8:30 AM-11:30 AM text:
//div[#class = "item-link" and .//div[#class = "title" and contains(., "8:30 AM-11:30 AM")]]

Categories