Get link in popup window (python, selenium) - python

I want to get a link in a popup window of this page.
http://stivconsultasexternas.cnbv.gob.mx/ConsultaInformacionEmisoras.aspx
Clicking the first link that appears the page show us a popup window. I like to get the link inside of thin window.
Here is my code. I appreciate any help.
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get("http://stivconsultasexternas.cnbv.gob.mx/ConsultaInformacionEmisoras.aspx")
link=driver.find_elements_by_partial_link_text(u"Verum")
time.sleep(5)
link[0].click()
time.sleep(5)
print driver.page_source.encode("utf_8","ignore")#Here I want a html of a popup window.
Thank you so much.

The problem you're running into is that the popup is in an iframe. Selenium can only access the current content, anything in a child iframe is inaccessible. In order to allow Selenium to access the new iframe, you'll need to use switch_to.frame:
link[0].click()
time.sleep(5)
driver.switch_to.frame("DefaultPlaceholder_callbackPanel_Popup_CIF-1")
print driver.page_source.encode("utf_8","ignore")#Here I want a html of a popup window.
driver.switch_to.default_content()
driver.find_element_by_xpath("//td[#id='DefaultPlaceholder_callbackPanel_Popup_HCB-1']").click()
Edit #1:
I looked at the page, and found the popin you wanted to access was contained in this iframe:
<iframe id="DefaultPlaceholder_callbackPanel_Popup_CIF-1" title="" src="/Detalle.aspx?enc=vxwQ2MoMebbVt2C93KNYJA%3d%3d" scrolling="auto" frameborder="0" style="height: 578px; width: 100%;">
switch_to.frame() can accept the following as an identifier:
- The name or id of the iframe. I used this particular case, since the iframe in question has id="DefaultPlaceholder_callbackPanel_Popup_CIF-1"
- The index of the iframe. That would looks something like switch_to.frame(0)
- The iframe element. You could find the iframe element via a find_element_by_ locator, then use the webelement result of that in switch_to.frame(el)
Edit #2:
To close the dialog, you need to use switch_to.default_content(), as the close button is outside of the iframe. You can then click the close button of the dialog, and proceed. See the last line in the code above, that line should close the dialog for you.

Related

xpath for click to a button - python/selenium

I'm trying to access a homepage, make login, click the login button and click a button (in the second page) using python/selenium.
The login button I wrote using Xpath and it's working well. The browser opens, writes user and login and click the login button.
Unfortunately, in the next page I'm not able to click on the button that I need to. It didn't work using Xpath and I can't understand why. The html is completely different from the first button, the button name is 'Reservar' and it is inside a class named <app-menu__menu>, written as:
<a href="/Services" id="advanced" class="element ">
<span>Reservar</span>
</a>
The xpath I got and tried:
xpath = "//*[#id="advanced"]"
Then I tried a second verion (it was gotten as the second line code xpath):
xpath = "//*[#id="advanced"]/span"
When I first tried to used them, I got an error. Then I change the "" to ' ' and the error was gone. But the program can't locate the button.
I'm using google-chrome, ubuntu, python3 and selenium package:
driver.find_element_by_xpath(xpath).click()
Thanks for any help.
You probably missing a delay / wait.
After clicking the submit / login button on the login page it takes some time to make the internal page loaded.
The recommended way to do that is to use Expected Conditions explicit waits, something like this:
wait.until(EC.visibility_of_element_located((By.XPATH, "//*[#id='advanced']"))).click()
To use the wait object here you will need to import the following inports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
And to initialize the wait object with
wait = WebDriverWait(driver, 20)
You will have to validate your locator is unique. As well as to validate the element you are trying to access is not inside iframe and not in a new window / tab etc.

Can't click element by xpath inside iFrame - Selenium

I'm aware that I have to switch to an iframe when I want to click and element inside it. Heres what the start of the iFrame looks like:
<iframe src="about:blank" name="tool_content" id="tool_content" class="tool_launch" allowfullscreen="allowfullscreen" webkitallowfullscreen="true" mozallowfullscreen="true" tabindex="0" title="Tool Content" style="height: 100%; width: 100%;" allow="geolocation *; microphone *; camera *; midi *; encrypted-media *; autoplay *" data-lti-launch="true"></iframe>
I switched to it using driver.switch_to.frame(driver.find_element_by_class_name("tool_launch"))
I then tried clicking a 'Join' button inside the Iframe by using it's exact xpath:
driver.find_element_by_xpath("/html/body/div[1]/div/div/div[2]/div/div/div[3]/div[1]/div[2]/div/div/div/div/div/div/div/div/div/div/table/tbody/tr/td[4]/div/div[1]/a").click()
But it keeps saying it's not present. Why? There are no further iframes inside that one, and this xpath is completely 100% inside the iframe. It can recognise other elements, so why not this one?
Here's the join button element line:
<a target="_blank" href="/lti/rich/j/96494344321?oauth_consumer_key=AAPUZMZcQCSN85nnG74vOQ&lti_scid=ee49a632bee823b3046456c29c5eb4064ee1e3790f433af82bebaf2752bde531" class="ant-btn ant-table-span" style=""> Join</a>
Heres what the iframe looks like - I want to click the join button:
It's inside a table.
I'm able to select things like the 'previous meetings' tab, but cannot use the Join or invitation buttons.
Here is the full HTML
I cannot paste it because this can only be viewed in inspect element (when viewing the source, you can't see all this), and I would only be able to copy one element at a time.
I have highlighted at the top where the start of the iframe is, and where the Join button that I want to click is.
The two images just continue on from one another.
Edit #1
I tried using the css selector - same result.
Try waiting and clicking on the element. Try not to use xpath.
elem=WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.ant-btn.ant-table-span")))
elem.click()
Import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

How would I locate a button element which are in the same list and have the same class but have different text on the button in selenium?

This is the html code from the website:
Screenshot from inspect element
Basically what I would like to do is to find the button but all of the buttons have the same class.
you can simply use the xpath and get the web element
WebElement button = driver.findElement(By.xpath("//button[text()='Buy $100']"));
According to Selenium documentation
You can locate it by xpath:
from selenium.webdriver.common.by import By
mytext = "special button"
driver.find_element(By.XPATH, '//button[text()="{}"]'.format(mytext))
To find a button that says "special button"

python selenium click on button

I am quite new to python selenium and I am trying to click on a button which has the following html structure:
<div class="b_div">
<div class="button c_button s_button" onclick="submitForm('mTF')">
<input class="very_small" type="button"></input>
<div class="s_image"></div>
<span>
Search
</span>
</div>
<div class="button c_button s_button" onclick="submitForm('rMTF')" style="margin-bottom: 30px;">
<input class="v_small" type="button"></input>
<span>
Reset
</span>
</div>
</div>
I would like to be able to click both the Search and Reset buttons above (obviously individually).
I have tried a couple of things, for example:
driver.find_element_by_css_selector('.button .c_button .s_button').click()
or,
driver.find_element_by_name('s_image').click()
or,
driver.find_element_by_class_name('s_image').click()
but, I seem to always end up with NoSuchElementException, for example:
selenium.common.exceptions.NoSuchElementException: Message: u'Unable to locate element: {"method":"name","selector":"s_image"}' ;
I am wondering if I can somehow use the onclick attributes of the HTML to make selenium click?
Any thoughts which can point me in the right direction would be great.
Thanks.
Remove space between classes in css selector:
driver.find_element_by_css_selector('.button .c_button .s_button').click()
# ^ ^
=>
driver.find_element_by_css_selector('.button.c_button.s_button').click()
try this:
download firefox, add the plugin "firebug" and "firepath"; after install them go to your webpage, start firebug and find the xpath of the element, it unique in the page so you can't make any mistake.
See picture:
browser.find_element_by_xpath('just copy and paste the Xpath').click()
For python, use the
from selenium.webdriver import ActionChains
and
ActionChains(browser).click(element).perform()
open a website https://adviserinfo.sec.gov/compilation and click on button to download the file and even i want to close the pop up if it comes using python selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from selenium.webdriver.chrome.options import Options
#For Mac - If you use windows change the chromedriver location
chrome_path = '/usr/local/bin/chromedriver'
driver = webdriver.Chrome(chrome_path)
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--disable-popup-blocking")
driver.maximize_window()
driver.get("https://adviserinfo.sec.gov/compilation")
# driver.get("https://adviserinfo.sec.gov/")
# tabName = driver.find_element_by_link_text("Investment Adviser Data")
# tabName.click()
time.sleep(3)
# report1 = driver.find_element_by_xpath("//div[#class='compilation-container ng-scope layout-column flex']//div[1]//div[1]//div[1]//div[2]//button[1]")
report1 = driver.find_element_by_xpath("//button[#analytics-label='IAPD - SEC Investment Adviser Report (GZIP)']")
# print(report1)
report1.click()
time.sleep(5)
driver.close()
I had the same problem using Phantomjs as browser, so I solved in the following way:
driver.find_element_by_css_selector('div.button.c_button.s_button').click()
Essentially I have added the name of the DIV tag into the quote.
The following debugging process helped me solve a similar issue.
with open("output_init.txt", "w") as text_file:
text_file.write(driver.page_source.encode('ascii','ignore'))
xpath1 = "the xpath of the link you want to click on"
destination_page_link = driver.find_element_by_xpath(xpath1)
destination_page_link.click()
with open("output_dest.txt", "w") as text_file:
text_file.write(driver.page_source.encode('ascii','ignore'))
You should then have two textfiles with the initial page you were on ('output_init.txt') and the page you were forwarded to after clicking the button ('output_dest.txt'). If they're the same, then yup, your code did not work. If they aren't, then your code worked, but you have another issue.
The issue for me seemed to be that the necessary javascript that transformed the content to produce my hook was not yet executed.
Your options as I see it:
Have the driver execute the javascript and then call your find
element code. Look for more detailed answers on this on
stackoverflow, as I didn't follow this approach.
Just find a comparable hook on the 'output_dest.txt' that will produce the same result, which is what I did.
Try waiting a bit before clicking anything:
xpath2 = "your xpath that you are going to click on"
WebDriverWait(driver, timeout=5).until(lambda x:
x.find_element_by_xpath(xpath2))
The xpath approach isn't necessarily better, I just prefer it, you can also use your selector approach.
I had the same problem and with Firefox, I got button element with the following steps:
right click button of interest and select "Inspect Accessibility Properties"
this opens the inspector. Right click the highlighted line and click "Print to JSON"
this opens a new tab. Look for nodeCssSelector and copy the value
This allowed me to accept cookies of the website Yahoo by using.
url = "https://yahoo.com"
driver = Firefox(executable_path="geckodriver.exe")
driver.get(url)
driver.find_element_by_css_selector("button.btn:nth-child(5)").click()
I tested this further and it allowed me to accept individual cookies with ease. Simply repeat the mentioned steps from before to get the button names.
url = "https://yahoo.com"
driver = Firefox(executable_path="geckodriver.exe")
driver.get(url)
driver.find_element_by_css_selector("a.btn").click()
driver.find_element_by_css_selector(".firstPartyAds > div:nth-child(2) > label:nth-child(1)").click()
driver.find_element_by_css_selector(".preciseGeolocation > div:nth-child(2) > label:nth-child(1)").click()
driver.find_element_by_css_selector("button.btn").click()
Another method is to
right click button of interest and select "Inspect"
right click the highlighted line and click "Copy -> CSS Selector" or whatever you need (there are multiple options, including XPath)
However, I think the second method may include whitespaces depending on what you copy, so you might need to manually remove (some of) them. The first method seems to be more foolproof, but I don't know if/how it works on other browsers than Firefox. The second method should work for all browsers.
Use This code To Click On Button
# finding the button using ID
button = driver.find_element_by_id(ID)
# clicking on the button
button.click()
e = driver.find_element(By.XPATH, 's_image').click()
sometime it does not work!
you can try:
e = driver.find_element(By.XPATH, 's_image') driver.execute_script("arguments[0].click();", e)

How to press a button with selenium and xpath

I need to press a button from a webpage with selenium,but that button has no id.
Other buttons from same webpage are having an id so no problem with pressing that ones with following code:
driver.find_element_by_id('some_id').click()
When i use firebug and firepath on that problem button output is:
<span class="linkRosso"> Invia truppe</span>
xpath:
html/body/div[6]/div[1]/div/div[3]/div/div[2]/table/tbody/tr/td[1]/a[3]/span
Is this information useful??
Use find_element_by_xpath.
xpath = 'html/body/div[6]/div[1]/div/div[3]/div/div[2]/table/tbody/tr/td[1]/a[3]/span'
driver.find_element_by_xpath(xpath).click()

Categories

Resources