Skip to content

Commit

Permalink
Merge pull request #198 from nikhil-sarin/replacephantomjs
Browse files Browse the repository at this point in the history
Replace phantomjs
  • Loading branch information
nikhil-sarin committed Mar 1, 2024
2 parents 6551dbb + e88b395 commit 3da859f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
34 changes: 31 additions & 3 deletions docs/installation.txt
Expand Up @@ -56,7 +56,7 @@ You are now ready to use redback. Please check out the `examples <https://github
Install :code:`phantomjs`
-------------------------

If you want to use :code:`redback` functionality to download Swift data you need to install phantomjs.
In previous releases of :code:`redback` if you wanted to use :code:`redback` functionality to download Swift data you needed to install phantomjs.
Installing phantomjs essentially requires that you first download the phantomjs file for your operating system from
the `website <https://phantomjs.org/download.html>`_. Then create a softlink or export the path to the bin file;
see discussion on `stackoverflow <https://stackoverflow.com/questions/36993962/installing-phantomjs-on-mac>`_.
Expand All @@ -75,5 +75,33 @@ Or use pkg installer if you are on linux.

phantomjs sometimes will not work with macOS or give a warning that it is no longer supported/verified.
In the short term, you can locate the file in your finder and right click to open it. Doing so once will allow the app to be used.
In the longer term, we are determining a replacement to drop the phantomjs dependency to download swift data.
Note phantomjs is not required for any other functionality of redback.
Note phantomjs is not required for any other functionality of redback.

Phantomjs is now replaced with ChromeDriver through selenium.

Install :code:`chromedriver` and :code:`selenium
-------------------------

In you want to use :code:`redback` functionality to download Swift data you now need to have Google Chrome installed and update selenium.
:code:`redback` is tested with Selenium 4.18.1 so please make sure you have this version or higher.

To test if your selenium/chrome installation is ready.
Please try the following code block.

.. code-block:: console

$ python
>>> from selenium import webdriver
>>> driver = webdriver.Chrome()
>>> driver.get("https://www.youtube.com/watch?v=dQw4w9WgXcQ")

If a page opens, then you are ready to start using :code:`redback` to download Swift data.

If nothing happens, you may need to install the ChromeDriver separately. You can do this via brew on MacOS

.. code-block:: console

$ brew install --cask chromedriver

And on Linux and Windows you can download the ChromeDriver and add it to your path from the official chromedriver webpage.
Note you do not have to do this ideally, as the ChromeDriver is included in the latest selenium package.
14 changes: 7 additions & 7 deletions redback/get_data/swift.py
Expand Up @@ -199,9 +199,9 @@ def download_flux_density_data(self) -> None:
driver = fetch_driver()
try:
driver.get(self.grb_website)
driver.find_element_by_xpath("//select[@name='xrtsub']/option[text()='no']").click()
driver.find_element("xpath", "//select[@name='xrtsub']/option[text()='no']").click()
time.sleep(20)
driver.find_element_by_id("xrt_DENSITY_makeDownload").click()
driver.find_element("id","xrt_DENSITY_makeDownload").click()
time.sleep(20)
grb_url = driver.current_url
# scrape the data
Expand All @@ -227,19 +227,19 @@ def download_integrated_flux_data(self) -> None:
# select option for BAT bin_size
bat_binning = 'batxrtbin'
if check_element(driver, bat_binning):
driver.find_element_by_xpath("//select[@name='batxrtbin']/option[text()='SNR 4']").click()
driver.find_element("xpath", "//select[@name='batxrtbin']/option[text()='SNR 4']").click()
# select option for subplot
subplot = "batxrtsub"
if check_element(driver, subplot):
driver.find_element_by_xpath("//select[@name='batxrtsub']/option[text()='no']").click()
driver.find_element("xpath","//select[@name='batxrtsub']/option[text()='no']").click()
# Select option for flux density
flux_density1 = "batxrtband1"
flux_density0 = "batxrtband0"
if (check_element(driver, flux_density1)) and (check_element(driver, flux_density0)):
driver.find_element_by_xpath(".//*[@id='batxrtband1']").click()
driver.find_element_by_xpath(".//*[@id='batxrtband0']").click()
driver.find_element("xpath",".//*[@id='batxrtband1']").click()
driver.find_element("xpath",".//*[@id='batxrtband0']").click()
# Generate data file
driver.find_element_by_xpath(".//*[@id='batxrt_XRTBAND_makeDownload']").click()
driver.find_element("xpath",".//*[@id='batxrt_XRTBAND_makeDownload']").click()
time.sleep(20)
grb_url = driver.current_url
driver.quit()
Expand Down
7 changes: 5 additions & 2 deletions redback/utils.py
Expand Up @@ -374,7 +374,7 @@ def check_element(driver, id_number):
checks that an element exists on a website, and provides an exception
"""
try:
driver.find_element_by_id(id_number)
driver.find_element('id', id_number)
except NoSuchElementException as e:
print(e)
return False
Expand Down Expand Up @@ -549,7 +549,10 @@ def frequency_to_bandname(frequency):

def fetch_driver():
# open the webdriver
return webdriver.PhantomJS()
options = webdriver.ChromeOptions()
options.add_argument("--headless=new")
driver = webdriver.Chrome(options=options)
return driver


def calc_credible_intervals(samples, interval=0.9):
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Expand Up @@ -2,7 +2,7 @@ numpy
setuptools
pandas
scipy
selenium==3.8.0
selenium==4.18.1
sncosmo
matplotlib
astropy
Expand Down

0 comments on commit 3da859f

Please sign in to comment.