I recently got help with downloading a PDF file from a popup form, but now I would like to exit the form and for some reason I can't perform any actions after downloading the PDF.
I've tried ActionChains(driver).send_keys(Keys.ESCAPE).perform() and it doesn't exit the form. I've also tried driver.back() but that goes back one page and I would like to stay on the current page to continue downloading files.
Here's the code so far:
options = webdriver.ChromeOptions()
prefs = {"download.default_directory":"C:/Users/gille/Documents/SJ Webscraping/data","plugins.plugins_list":[{"enabled":False,"name":"Chrome PDF Viewer"}]}
options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(options=options)
driver.get("https://www.southtechhosting.com/SanJoseCity/CampaignDocsWebRetrieval/Search/SearchByElection.aspx")
driver.find_element_by_xpath('//*[#id="ctl00_DefaultContent_ASPxRoundPanel1_btnFindFilers_CD"]').click()
driver.find_element_by_xpath('//*[#id="ctl00_GridContent_gridFilers_DXCBtn0"]').click()
driver.find_elements_by_xpath('//td[#class="dxgvCommandColumn_Glass dxgv"]//img[#title="View Form"]')[0].click()
driver.switch_to.frame(driver.find_element_by_tag_name('iframe'))
a = driver.find_element_by_link_text("Click here")
ActionChains(driver).key_down(Keys.CONTROL).click(a).key_up(Keys.CONTROL).perform()
#insert exit sequence here
Trying to exit out of the following popup form:
I had updated my answer here, but incase you missed it,
Try this to close the popup:
driver.switch_to.default_content()
driver.find_element_by_xpath('//*[#id="ctl00_GenericPopupSizeable_InnerPopupControl_HCB-1"]/img').click()
Explanation: The close button of popup is not in the iframe where download link is seen, so we need to go back to main content and then click on popup close button.
Related
I am trying to automate downloading of movies with magnet links using the BitTorrent web UI. I can click on the 'add torrent link' button and the popup does appear but after that, the code fails as it is unable to find the element where the torrent link needs to be added. The same problem occurs when I try to input the file location. I tried time.sleep but had no luck.
My code snippet:
def torrent(path, n):
#link to web UI
driver.get("http://127.0.0.1:8080/")
#default login credentials
username = driver.find_element_by_xpath("//*[#id='username']")
username.send_keys("admin")
password = driver.find_element_by_xpath("//*[#id='password']")
password.send_keys("adminadmin")
time.sleep(2)
driver.find_element_by_xpath("//*[#id='login']").click()
time.sleep(2)
#the elements I am trying to find
driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/a[1]").click()
time.sleep(5)
location = driver.find_element_by_xpath("/html/body/form/div/fieldset/table/tbody/tr[2]/td[2]")
location.send_keys(path)
input = driver.find_element_by_xpath("/html/body/form/div/textarea")
i = 0
while i <= n-1:
input.send_keys(list_torrent[i])
If you need any other information please let me know. I tried using BitTorrent API already but with no luck. The HTML page has a hidden overflow but it shouldn't be a problem since I am clicking on the elements which should make my code visible.
Thanks in advance.
Looks like it is in iframe.
switch to iframe like this :
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH," Ifrmae xpath here")))
and then do the other stuff here :
once done with iframe switch it back to
default_content like this :
driver.switch_to.default_content()
I need to scrape the CSV result of the website (https://requestmap.webperf.tools/). But I can’t.
This is the process I need to do:
1- load the website (https://requestmap.webperf.tools/)
2- enter a new website as an input (for example, https://stackoverflow.com/)
3- click submit
4- in the new page which opens after the submit, download the csv file at the end of the page
But I think the driver has the main page and doesn’t switch to the new page. That's why I can’t download the CSV file.
Would you please tell me how to do it?
here is my code:
options = webdriver.ChromeOptions()
options.add_argument('-headless')
options.add_argument('-no-sandbox')
options.add_argument('-disable-dev-shm-usage')
driver = webdriver.Chrome('chromedriver',options=options)
driver.get("https://requestmap.webperf.tools/")
driver.find_element_by_id("url").send_keys("https://stackoverflow.com/")
driver.find_element_by_id("submitter").click()
I have tested different ways to solve my issue:
First of all:
I got this code from here :
But it doesn't work.
window_after = driver.window_handles[1]
driver.switch_to.window(window_after)
I also tried this or this, but they are not working as well.
# wait to make sure there are two windows open
# it is not working
WebDriverWait(driver, 30).until(lambda d: len(d.window_handles) == 2)
# switch windows
# it is not working
driver.switch_to_window(driver.window_handles[1])
content = driver.page_source
soup = BeautifulSoup(content)
driver.find_elements_by_name("Download CSV")
So How can I get this issue solved?
Is there any other way in python to do so and switch to a new windows?
I want to press what I thought was a simple button on a website to download a CSV file.
However, I cannot find the button to press in the HTML code and, if I can, it looks like it carries some parameters (if it is the class "input-group-addon btn").
I've done similar but limited stuff like this before, but this looks different. I can find other buttons to press on this website but not this one that downloads the CSV file. It does not contain a address to the file and it is definitely not a "normal" button. I struggle to find some info online that is not about either a link to the file address or a normal button.
from selenium import webdriver
import time
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument("--test-type")
options.binary_location = "/usr/bin/chromium"
driver = webdriver.Chrome(chrome_options=options)
driver.get('https://www.tennet.eu/electricity-market/transparency-pages/transparency-germany/network-figures/actual-and-forecast-wind-energy-feed-in/')
text_area1 = driver.find_element_by_id('daterange')
text_area1.send_keys("print('23.05.2019 - 23.05.2019')")
submit_button = driver.find_elements_by_xpath('SOMETHING IN HERE')
submit_button.click()
Goal for this part of the script is to open site, input today's date in a textfield then press a button to download CSV file.
Here is how you could do it :
import time
text_area1 = driver.find_element_by_id('daterange')
text_area1.clear()
text_area1.send_keys("11.05.2019 - 12.05.2019")
#Wait for page to update
time.sleep(1)
#Click submit button
driver.find_element_by_css_selector('.show-calendar .applyBtn').click()
time.sleep(1)
#Download file
driver.find_element_by_css_selector('.icon-download').click()
EDIT
Using css selectors instead of xpath
The exact button I am trying to click is this:
https://www.bungol.ca/map/location/toronto/?
close the property slider on the left
click tool -> open list
click on any of the listing and you get to something like this:
Image for close button
Here's what I tried to do:
time.sleep(5) #wait for page to fully load
driver.find_element_by_xpath("""/html/body/div[17]/div/div/div/button""").click()
and
time.sleep(5) #wait for page to load
driver.find_element_by_css_selector("""html body#body.modal-open div#listingInfoModal.modal.fade.show div.modal-dialog.modal-xl div.modal-content div.modal-body button.close""").click()
This CSS selector will do it.
#listingInfoModal button.close
I am trying to get some comments off the car blog, Jalopnik. It doesn't come with the web page initially, instead the comments get retrieved with some Javascript. You only get the featured comments. I need all the comments so I would click "All" (between "Featured" and "Start a New Discussion") and get them.
To automate this, I tried learning Selenium. I modified their script from Pypi, guessing the code for clicking a link was link.click() and link = broswer.find_element_byxpath(...). It doesn't look liek the "All" button (displaying all comments) was pressed.
Ultimately I'd like to download the HTML of that version to parse.
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time
browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://jalopnik.com/5912009/prius-driver-beat-up-after-taking-out-two-bikers/") # Load page
time.sleep(0.2)
link = browser.find_element_by_xpath("//a[#class='tc cn_showall']")
link.click()
browser.save_screenshot('screenie.png')
browser.close()
Using Firefox with the Firebug plugin, I browsed to http://jalopnik.com/5912009/prius-driver-beat-up-after-taking-out-two-bikers.
I then opened the Firebug console and clicked on ALL; it obligingly showed a single AJAX call to http://jalopnik.com/index.php?op=threadlist&post_id=5912009&mode=all&page=0&repliesmode=hide&nouser=true&selected_thread=null
Opening that url in a new window gets me the comment feed you are seeking.
More generally, if you substitute the appropriate article-ID into that url, you should be able to automate the process without Selenium.