EzDevInfo.com

splinter

splinter - python test framework for web applications Splinter — Splinter 0.7.3 documentation

Can I somehow select a specific element from dropdown list on the page via splinter module in Python

Can I somehow select a specific element from dropdown list on the page via splinter module in Python?

I have the following HTML code:

<select id="xyz">
   <optgroup label="Group1">
      <option value="1">pick1</option>
      <option value="2">pick2</option>
   </optgroup>
   <optgroup label="Group2">
       <option value="3">pick3</option>
       <option value="4">pick4</option>
   </optgroup>
</select>

Suppose that I need to select "pick3" option. How can I do it?


Source: (StackOverflow)

Opening a Splinter Browser using Selenium

When I initiate a splinter browser object for Chrome, I would get a yellow banner saying "You are using an unsupported command-line flag..." I found a way to get rid of that using selenium.

browser = Browser('chrome')
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["ignore-certificate-errors"])
browser = webdriver.Chrome(chrome_options=options)

As you can see, it is actually initiating the browser twice, but I only want Chrome to pop up once. Is there a way to launch the browser of the Splinter object using Selenium?


Source: (StackOverflow)

Advertisements

Verify that no resource or JavaScript errors occurred

I need to verify, that no JavaScript exceptions or resource errors occur during our tests.

For phantomjs, there is for example an onResourceError event: http://phantomjs.org/api/webpage/handler/on-resource-error.html

With splinter, is it possible, to have similar assertions? Like, for example, no resource error occurred after clicking the next button in a gallery, or no JavaScript exceptions occur after clicking a random option in the navigation bar.


Source: (StackOverflow)

filling in input fields with splinter

I am trying to fill in fields on a login form with splinter. When I examine the rendered page, I see that the username input box has both a tag and a name of "u". How can I fill in this field from splinter? I tried the following:

from splinter import Browser

url = "http://www.weiyun.com/disk/login.html"
browser = Browser('firefox')
browser.visit(url)
browser.fill("u", "foo@bar.com")
print "done"

But there is no such field according to the error returned:

ElementDoesNotExist: no elements could be found with name "u"

How does one fill in the input fields on pages like this using splinter?


Source: (StackOverflow)

How can i get text between using python splinter lib

I want get the text between I have already use lib splinter

"<span id="stWelcomeInbox" class="nui-txt-impt">
1
</span>"

PS: i don't want to download the page and read as html code maybe re lib is available but i don't know how to use it without download that page.i don't know how to use re lib ,too. please help me


Source: (StackOverflow)

How to submit a POST payload with splinter broswer in python

I am working with pythons splinter browser library, opening a page and passing data for that page to process in the GET string. I have a need to submit a POST payload because the length of the variables in my GET string is at times exceeding the max GET string length.

I have searched around extensively but can not find a syntax example or anything saying there is a way to send POST data without clicking submit on a form.

Any help would be much appreciated.

Thanks


Source: (StackOverflow)

Get href value in Splinter?

I would like to get href value from <a> element in Splinter.

Is there any api method for that?


Source: (StackOverflow)

Splinter, opens up browser but won't visit webpage

from splinter import Browser 
browser = Browser()
browser.visit('http://www.google.com/')

Straight from their tutorial, and all it does is open up a window, and I do not recieve any errors. I've reinstalled several times, and if it opens up a browser window and I receive no errors, it's obviously installed correctly, so why is this happening?


Source: (StackOverflow)

Splinter or Selenium: Can we get current html page after clicking a button?

I'm trying to crawl the website "http://everydayhealth.com". However, I found that the page will dynamically rendered. So, when I click the button "More", some new news will be shown. However, using splinter to click the button doesn't let "browser.html" automatically changes to the current html content. Is there a way to let it get newest html source, using either splinter or selenium? My code in splinter is as follows:

import requests
from bs4 import BeautifulSoup
from splinter import Browser

browser = Browser()
browser.visit('http://everydayhealth.com')
browser.click_link_by_text("More")

print(browser.html)

Based on @Louis's answer, I rewrote the program as follows:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Firefox()
driver.get("http://www.everydayhealth.com")
more_xpath = '//a[@class="btn-more"]'
more_btn = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_xpath(more_xpath))
more_btn.click()
more_news_xpath = '(//a[@rel='nofollow' href="http://www.everydayhealth.com/recipe-rehab/5-herbs-and-spices-to-intensify-flavor.aspx"])[2]'
WebDriverWait(driver, 5).until(lambda driver: driver.find_element_by_xpath(more_news_xpath))

print(driver.execute_script("return document.documentElement.outerHTML;"))
driver.quit()

However, in the output text, I still couldn't find the text in the updated page. For example, when I search "Is Milk Your Friend or Foe?", it still returns nothing. What's the problem?


Source: (StackOverflow)

How to find element value using Splinter?

I have following piece of html:

<p class="attrs"><span>foo:</span> <strong>foo</strong></p>
<p class="attrs"><span>bar:</span> <strong>bar</strong></p>
<p class="attrs"><span>foo2:</span> <strong></strong></p>
<p class="attrs"><span>description:</span> <strong>description body</strong></p>
<p class="attrs"><span>another foo:</span> <strong>foooo</strong></p>

I would like to get description body using splinter. I've managed to get a list of p using

browser.find_by_css("p.attrs")

Source: (StackOverflow)

Django with splinter and phantomjs is painfully slow

Today I tried combining django's LiveServerTestCase with splinter and phantomjs webdriver.

Here's what I do (simplified version):

class Test(LiveServerTestCase):

    def setUp(self):
        self.browser = Browser('phantomjs')

    def tearDown(self):
        self.browser.quit()

    def test(self):
        self.browser.visit(self.live_server_url)
        self.assertIn("Hello world!", self.browser.title)

Sometimes tests run fine - even though taking a second per test method to execute. But sometimes it can randomly take ~100 seconds for that single test method to execute, or it just freezes until I am out of patience to wait for it to finish.

I use django_nose as a test runner, and I pass --liveserver=localhost:8081-8181 range of ports to ./manage.py test command.

Is there any way to speed it up? Is there other web test runner I can which is faster?

Default web driver seem to be more reliable speed-wise (1-3 seconds per test method), but it's still pretty slow. I also would prefer a headless browser for testing.


Source: (StackOverflow)

splinter: faster way to find elements?

so I'm using the python splinter library to test a web app and a problem I am running into is following when I am checking if an element exists and I'm manually finding each to manipulate it.

The problem is that when the inputs list gets bigger than 4 items or more and it runs into case when the element doesn't exist, it takes 12+ seconds to complete.

I also tried setting wait_time=1, but if inputs list is bigger than 10, it would take 10 times in total should the element not exist anywhere on the page.

for i in inputs:
    if browser.element_exists():
        elm = browser.find_element():
        elm.text()

I need some way to speed this up so that this element checking happens in parallel, without one by one. The only thing I can think of is executing javascript which I don't like (I'd like to keep it all in python).

def get_columns(current_depth,step,element):
    columns = []
    for xpath in xpaths:
        what = parse_xpath(row[2])
        if browser.is_element_present_by_xpath(xpath,wait_time=1):
            element = browser.find_by_xpath(xpath)
            columns.append(element.text)
        else:
            columns.append('none')
    return columns

Source: (StackOverflow)

How do I save a webpage to the disk for later scrapping using splinter library?

I have the following python code using splinter library for searching a specific term in a website:

from splinter import Browser
browser = Browser()

browser.visit("http://decs.bvs.br/cgi-bin/wxis1660.exe/decsserver/?IsisScript=../cgi-bin/decsserver/decsserver.xis&interface_language=p&previous_page=homepage&previous_task=NULL&task=start")
browser.choose('search_language','p')
browser.fill('search_exp','costas')
element = browser.find_by_name("consult_button")
element.click()

And it works, the firefox page opens the page with the results. However I have not found a way to save those results as a html file to disk in orde to scrape them for terms. How do you save the webpage to disk using splinter?

Thanks in advance


Source: (StackOverflow)

Django LiveServerTestCase with phantomjs intermittent hangs / timeouts

I'm using Django (1.5.5), selenium (2.41.0), splinter (0.6.0) and phantomjs (1.9.7) for running live tests.

While the tests mostly work, every now and then (very often on CircleCI, less often in a local VM) they hang until either there's a timeout on CircleCI or I kill the runner manually (Ctrl-C ie. KeyboardInterrupt works).

This is how my base test class looks:

class SplinterTestCase(LiveServerTestCase):
    @classmethod
    def setUpClass(cls):
        super(SplinterTestCase, cls).setUpClass()
        # start phantom just once per class, to speed up tests
        cls.phantom = splinter.Browser('phantomjs', load_images=False)

    @classmethod
    def tearDownClass(cls):
        cls.phantom.quit()
        super(SplinterTestCase, cls).tearDownClass()

    def login(self, *args, **kwargs):
        # perform a login using Django builtin "client", steal the session
        # cookie and inject it to phantomjs, avoiding the need to do the
        # login dance for each test
        from django.conf import settings
        cn = settings.SESSION_COOKIE_NAME

        self.django_client.login(*args, **kwargs)
        if cn in self.django_client.cookies:
            self.client.driver.add_cookie({
                'name': cn,
                'value': self.django_client.cookies[cn].value,
                'path': '/',
                'domain': 'localhost'
            })

    def setUp(self):
        # use phantom as the test client instead of Django's
        super(SplinterTestCase, self).setUp()
        self.django_client = self.client
        self.client = self.phantom

    def tearDown(self):
        # this seems to help somewhat (decreases the number of timeouts), but
        # doesn't solve it completely
        self.client.visit('about:config')
        super(SplinterTestCase, self).tearDown()

After Ctrl-C, this is the stacktrace I get:

Traceback (most recent call last):
  File "/usr/lib/python2.7/wsgiref/handlers.py", line 86, in run
    self.finish_response()
  File "/usr/lib/python2.7/wsgiref/handlers.py", line 127, in finish_response
    self.write(data)
  File "/usr/lib/python2.7/wsgiref/handlers.py", line 215, in write
    self._write(data)
  File "/usr/lib/python2.7/socket.py", line 324, in write
    self.flush()
  File "/usr/lib/python2.7/socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 104] Connection reset by peer
Traceback (most recent call last):
  File "/home/ubuntu/memo-angel/venv/local/lib/python2.7/site-packages/django/test/testcases.py", line 998, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 310, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 323, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/home/ubuntu/memo-angel/venv/local/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 150, in __init__
    super(WSGIRequestHandler, self).__init__(*args, **kwargs)
  File "/usr/lib/python2.7/SocketServer.py", line 640, in __init__
    self.finish()
  File "/usr/lib/python2.7/SocketServer.py", line 693, in finish
    self.wfile.flush()
  File "/usr/lib/python2.7/socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe

A similar problem might've been discussed in Django with splinter and phantomjs is painfully slow as the original poster also mentioned "it just freezes until I am out of patience to wait for it to finish". The answer there mentioned to try to put phantomjs start/stop in class setup/teardown, which I did here, but it doesn't solve the problem.

Has anyone experienced a similar problem, and if you have, what are your workarounds?


Source: (StackOverflow)

TDD for plupload with Django/Splinter

I'm trying to set up tests for a upload using the plupload queue widget. I'm using Splinter for in-browser test, but I couldn't find a way to make it happen. Splinter has some methods to attach files, but only if it's a simple file field. Another way would be click the button to browse the files, and choose the file... but I don't think it's possible using Splinter (or selenium), is it? Or with drag-n-drop of the files.

Anyone has any suggestion of the best way to automatize theses tests?


Source: (StackOverflow)