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
Related
I am trying to navigate to a search box and send_keys with selenium python but completely stuck.
And here is the source code snippet:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<div id="LeftTreeFrame" class="leftNavBackground" >
<div class="ui-widget searchPanelContainer">
<div id="searchPanel" class="search-field-container search-field-container-margin">
<input type="text" doesntDirty id="Search" name="Search" class="search-text-field-left-tree-frame" NoHighlight="nohighlight"/>
<div class="search-field-icon-container">
<a id="searchlbl" href="#"><img src="../images/normal_search_u39.svg" title="Go To Page" /></a>
</div>
</div>
</div>
<div id='pageNavigation'>
<div id='ootbNavigationPage'></div>
<div id='favoriteNavigationPage'></div>
<div id='adminNavigationPage'></div>
<div id='navigationEmptyState' class="treeEmptyState">
<div class="message"></div>
</div>
</div>
<div class="navigation-view-mode-container">
<div class="box" onclick="renderModel(0)">
<button type="button">
<span class="svg-load ootb-icon" data-src="~/images/Reskin/ootb-icon.svg"></span>
</button>
</div>
<div class="star" onclick="renderModel(1)">
<button type="button">
<span class="svg-load star-icon" data-src="~/images/Reskin/star.svg"></span>
</button>
</div>
<div class="person" onclick="renderModel(2)">
<button type="button">
<span class="svg-load person-icon" data-src="~/images/Reskin/person-nav.svg"></span>
</button>
</div>
</div>
</div>
When I try to do
element = driver.find_element(By.XPATH, '//input[#name="Search"]')
element.send_keys('test')
I get error "selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable"
I have tried everything I can imagine, but cannot click the element or send keys.
Also, this page is a new page that opens after the last successful click. I first tried switching to this page by
#printing handles
handles = driver.window_handles
i=0
for handle in handles:
print(f"Handle {i}: {handle}\n")
i +=1
#after confirming new page is second handle via:
driver.switch_to.window(handles[1])
print(f" Title: {driver.title}")
print(f" Current url: {driver.current_url}")
print('\n')
#I can even find the tag I am looking for after switching to new window:
all_div_tags = driver.find_elements(By.TAG_NAME, "input")
for tag in all_div_tags:
print(f"Attribute name: {tag.get_attribute('name')}\n")
#but i cannot get to the search box. Thank you in advance!
Look at the html code, notice that //input[#name="Search"] is contained in an <iframe>. In order to select an element inside an iframe with find_element() you have first to switch to the iframe, as shown in the code
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "frmCode")))
element = driver.find_element(By.XPATH, '//input[#name="Search"]')
...
I am trying to click into a radio button inside a span. Trying to execute:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chromeDriver = webdriver.Chrome()
chromeDriver.get('https://www.flytap.com/pt-br/')
#Works Fine
chromeDriver.find_element_by_id('origin')
origin.click()
origin.clear()
origin.send_keys('(RIO) Rio De Janeiro - todos os aeroportos , Brasil')
origin.click()
#Where I got the error
milesBox = chromeDriver.find_element_by_id("booking-type-2").click()
#Also tried:
milesBox = chromeDriver.find_element_by_id("booking-type-2").send_keys(Keys.ENTER)
#And, finally:
milesBox = chromeDriver.find_element_by_id("booking-type-2").send_keys(Keys.SPACE)
Here is the error I got:
ElementNotVisibleException: Message: element not visible
HTML code:
<!-- Payment Methods START -->
<fieldset id="booking-payment-drag" class="booking-payment">
<div class="toolbar-radio-wrapper">
<legend class="ipt-label">Reservar com:</legend>
</div>
<div class="toolbar-radio-wrapper">
<div class="radio">
<span class="checked"><input type="radio" id="booking-type-1" name="booking-type" value="1" checked></span>
</div>
<label for="booking-type-1" class="ipt-label">Dinheiro</label>
</div>
<div class="toolbar-radio-wrapper js-country-not-eligible">
<div class="radio">
<span class=""><input type="radio" id="booking-type-2" name="booking-type" value="2"></span>
</div>
<label for="booking-type-2" class="ipt-label">Milhas</label>
</div>
<div class="toolbar-radio-wrapper">
<div class="radio is-disabled js-country-eligible">
<span class=""><input type="radio" id="booking-type-3" name="booking-type" value="3" disabled></span>
</div>
<label for="booking-type-3" class="ipt-label">Miles&Cash</label>
</div>
</fieldset>
As per the HTML you have shared to click on the radio button beside the <label> with text as Milhas you can use the following line of code :
chromeDriver.find_element_by_xpath("//input[#id='booking-type-2' and #name='booking-type']").click()
You can use this xpath
//div[#class='radio']//span//input[#id='booking-type-1']
//div[#class='radio']//span//input[#id='booking-type-2']
//div[#class='radio is-disabled js-country-eligible']//span//input[#id='booking-type-3']
It seems like the origin element's dropdown covers the radio button you are trying to click. Maybe you could change the order of operations though? Clicking the destination element also opens up the section where the radio button is but it doesn't seem to cover it. This is a quick example of clicking that radio button and then you could go fillout the origin input after.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.flytap.com/pt-br/')
destination = driver.find_element_by_id('destination')
destination.click()
miles_box = driver.find_element_by_id('booking-type-2')
miles_box.click()
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_xpath(".//*"):
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_tag_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
I am trying to interact with a sign out button but I just can't seem to click on it using link text or xpath.
I have tried following these answers with no luck:
Why Cant I Click an Element in Selenium?
Unable to click link using selenium webdriver in python
Here is my code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from xatu.tests.base import login
from xatu.tests.bagon import BagonBaseTestCase
class BasicTestCase(BagonBaseTestCase):
#login
def test_logout(self):
self._wait_until_id_presents("quotes-form")
WebDriverWait(self, 10).until(
EC.presence_of_element_located((By.XPATH, "//a[#href='/login/clear']/i")))
self.browser.find_element_by_xpath("//a[#href='/login/clear']/i").click()
self.implicitly_wait(2)
self._title_check("Login")
The first line under test_logout calls a function that waits for a certain element to appear on the webpage so I can see that the page has been loaded. Then I try to click the sign out button.
This is the HTML(element is at class="btn" href="/login/clear"):
<div class="navbar navbar-fixed-top screen-only">
<div class="navbar-inner">
<div class="container-fluid">
<a class="btn btn-navbar" data-target=".nav-collapse" data-toggle="collapse">
<a class="brand" href="/">
<div class="nav-collapse collapse">
<ul class="nav">
<p class="pull-right" style="margin-top: 4px; margin-left: 4px;">
<a class="btn" href="/login/clear">
<i class="icon-off"/>
Sign out
</a>
</p>
<p class="navbar-text pull-right"> Logged-in as D. V. Lauper </p>
</div>
<!--/.nav-collapse -->
</div>
</div>
</div>
When I try finding by link_text, the element can't be found. Running this code gives me a stacktrace error saying:
ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Edit: I've tried Saifur's answer and updated my code to his answer but now I get: AttributeError: 'BasicTestCase' object has no attribute 'find_element'. I've tried changing "self" to "self.browser" as an argument in WebDriverWait() but I would then get my original error.
Use explicit wait and relativexpath
like //a[#href='/login/clear']/i
from xatu.tests.base import login
from xatu.tests.bagon import BagonBaseTestCase
class BasicTestCase(BagonBaseTestCase):
#login
def test_logout(self):
self._wait_until_id_presents("quotes-form")
WebDriverWait(self, 10).until(
EC.presence_of_element_located((By.XPATH, "//a[#href='/login/clear']/i")))
self.browser.find_element_by_xpath("//a[#href='/login/clear']/i").click()
self.browser.implicitly_wait(2)
self._title_check("Login")
You need an explicit wait. See docs. Example code:
element = WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.XPATH,'//a[#href="/login/clear"]')))
And then just click the element.
element.click()
Note I added my answer b/c -- in my tests at least -- you don't need to worry about the italics tag. That's for the text, not the button -- and you're not clicking the text. So find By.XPATH and select a unique attribute (i.e., the href in this case and very likley not the class attr), and then click your element.
EDIT:
please try this line:
element = WebDriverWait(self.browser, 30).until(EC.visibility_of_element_located((By.XPATH,'//a[#class="btn" and #href="/login/clear"]')))
I clicked on a link which opens a small window/popup/iframe and the popup window has Xpath:
//html/body/div[2]
ie. it has no window_id but in firebug the identifier shows the window as a <div class="some_name"> so I'm assuming it's a popup window; correct me if I'm wrong.
When I use the selectPopup of selenium IDE it works fine in switching from the main window to the popup/new_window but when exporting to webdriver-python it doesn't have that option. So I tried using driver.switch_to_window and switch_to_frame along with the xpath mentioned above but no luck ie. driver.switch_to_frame(driver.find_element_by_xpath("//html/body/div[2]"))
Error thrown : NoSuchElementException and that's because it's not able to select the iframe.
Since it's working fine in Selenium IDE I exported it to python-webdriver which converted the wait_for_element clause to
for i in range(60):
try:
if self.is_element_present(By.XPATH, "//*[#id='heading']/div[2]/div/div/ul/li[2]/a"): break
except: pass
time.sleep(1) else: self.fail("time out")
which returns the above error
Detailed html:
//*[#id='heading']/div[2]/div/div/ul/li[2]/a is the xpath of the element and as html this is what it is Home and in detail:
`
<div class="help">
<div class="page-header">
<div id="heading">
<div id="search">
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<ul class="nav">
<li class="active">
<li>
Home
</li>
<li>
<li>
<li>
</ul>
<form class="navbar-form pull-right">
</div>
</div>
</div>
</div>`
try out this :
new Actions(driver).click(driver.findElement(By.xpath("//div[#id='pop-up-window']"))).perform();