User input produces blank file with no result

时间:2017-08-04 11:57:06

标签: selenium user-input raw-input

I am playing around with Selenium to get screenshots of websites to view them safely.

The original code I found and tweaked was like this and works.

from selenium import webdriver
br = webdriver.PhantomJS()
br.get('http://www.google.com')
br.save_screenshot('screenshot.png')
br.quit

It gives you a screenshot of the website

I wanted to get user input so that I did not have the VI the file overtime I need a screenshot of a URL this is what I changed the code too.

#!/usr/bin/python
import re
import sys
from selenium import webdriver
br = webdriver.PhantomJS()
br.get_user_input =raw_input('Enter URL:')
br.save_screenshot('screenshot.png')
br.quit

Now it ask for the URL and you input it in the program runs and finishes and even creates the screenshot.png file but its blank

2 个答案:

答案 0 :(得分:0)

try to use a chrome canary (if you need headless browser). you don't have to use a selenium.

make_screen = '''#!/usr/bin/env bash
    test -f ./chrome-linux/chrome && echo "chrome exists" || unzip chrome-linux.zip
    ./chrome-linux/chrome --headless --disable-gpu --virtual-time-budget=7000 --hide-scrollbars --screenshot=dir/screens/{screen_prod}.png --window-size=1200,2000 {link_prod}'''.format(screen_prod = screen_prod, link_prod = link_prod)

答案 1 :(得分:0)

Here is the Answer to your Question:

There is a small bug in your code. You havn't called the get() method with your WebDriver instance br.

As a solution, (to avoid Law of Demeter) I have broken up that line in to two. Took the url from the user as an input in the first line. In the next line passed the url as an argument to get() method. Next it takes a proper snapshot and saves it within Screenshots sub-directory. Here is the working code block :

from selenium import webdriver
br = webdriver.PhantomJS(executable_path="C:\\Utility\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe")
user_input = input('Enter URL : ')
br.get(str(user_input))
br.save_screenshot('./Screenshots/my_next_screenshot.png')
br.quit

The output on my console is:

Enter URL : http://google.com
Process finished with exit code 0

Let me know if this Answers your Question.