Posts: 404
Threads: 94
Joined: Dec 2017
Trying to run this simple code ( which should be a part of longer script ):
1 2 3 4 5 6 7 8 9 |
from selenium import webdriver
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options
opts = Options()
opts.set_headless()
assert opts.headless
browser = webdriver.Firefox(options = opts)
|
...but from some reason it won't open duckduckgo web site.
Posts: 5,151
Threads: 396
Joined: Sep 2016
try adding headless=True in the argument of set_headless as show in this tutorial
https://python-forum.io/Thread-Web-scraping-part-2
Recommended Tutorials:
Posts: 12,039
Threads: 487
Joined: Sep 2016
Oct-14-2018, 12:15 AM
(This post was last modified: Oct-14-2018, 12:15 AM by Larz60+.)
Do you get any errors?
I tried it with chromedriver and it worked fine (although code a bit different (see below)):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import os
class TryDucky:
def __init__( self ):
os.chdir(os.path.dirname(__file__))
self .duck()
def duck( self ):
chrome_options = Options()
chrome_options.add_argument( "--headless" )
chrome_options.add_argument( '--disable-gpu' )
chrome_options.add_argument( '--log-level=3' )
browser = webdriver.Chrome(chrome_options = chrome_options)
search_comment = browser.find_element_by_xpath( '/html/body/div/div[2]/div/div/div[3]/div/div' )
print ( f '\n{search_comment.text}' )
if __name__ = = '__main__' :
TryDucky()
|
Don't forget if headless, you won't see anything until you actually print it.
result:
Output: The search engine that doesn't track you. Help Spread DuckDuckGo!
options part of code courtesy snippsat
Posts: 404
Threads: 94
Joined: Dec 2017
Oct-14-2018, 11:29 PM
(This post was last modified: Oct-14-2018, 11:30 PM by Truman.)
(Oct-14-2018, 12:04 AM)metulburr Wrote: try adding headless=True in the argument of set_headless as show in this tutorial
https://python-forum.io/Thread-Web-scraping-part-2 added it and it's struggling to execute the code. After a while I receive this:
Error: C:\Python36\kodovi>sel6.py
Traceback (most recent call last):
File "C:\Python36\kodovi\sel6.py", line 9, in <module>
browser = webdriver.Firefox(options=opts)
File "C:\Python36\lib\site-packages\selenium\webdriver\firefox\webdriver.py",
line 174, in __init__
keep_alive=True)
File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 157, in __init__
self.start_session(capabilities, browser_profile)
File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 321, in execute
self.error_handler.check_response(response)
File "C:\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py"
, line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: connection refused
larz60+, I'm looking at your code and it looks a bit above my league.
Posts: 5,151
Threads: 396
Joined: Sep 2016
Check your geckodriver.log file (should be in the same directory as python file)
If it says Error: GDK_BACKEND does not match available displays then install pyvirtualdisplay:
1 |
pip install pyvirtualdisplay selenium
|
You might need xvfb too:
Recommended Tutorials:
Posts: 404
Threads: 94
Joined: Dec 2017
metulburr, I installed pyvirtualdisplay but still get the same error.
Not sure what did you mean by xvfb, didn't add anything...
Posts: 7,324
Threads: 123
Joined: Sep 2016
Oct-15-2018, 01:00 AM
(This post was last modified: Oct-15-2018, 01:00 AM by snippsat.)
You may need to update FireFox,Selenium and geckodriver.
In first post you use older code.
opts.set_headless() is Deprecated,but it still work.
If you don't get error with first code,can try to get something back.
Remember headless mean that you don't see anything as browser dos not start.
set_headless(headless=True) to see browser set_headless(headless=False)
And comment out #assert opts.headless.
1 2 3 4 5 6 7 8 9 10 11 |
from selenium import webdriver
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options
opts = Options()
opts.set_headless()
assert opts.headless
browser = webdriver.Firefox(options = opts)
logo = browser.find_elements_by_css_selector( '#logo_homepage_link' )
print (logo[ 0 ].text)
|
Output: About DuckDuckGo
FireFox has messed a lot with setting for there driver,so updated browser may need this.
Both this method testet here work fine for me.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import time
options = Options()
options.add_argument( "--headless" )
caps = webdriver.DesiredCapabilities().FIREFOX
caps[ "marionette" ] = True
browser = webdriver.Firefox(firefox_options = options, capabilities = caps, executable_path = r "geckodriver.exe" )
logo = browser.find_elements_by_css_selector( '#logo_homepage_link' )
print (logo[ 0 ].text)
|
Output: About DuckDuckGo
Posts: 404
Threads: 94
Joined: Dec 2017
Oct-15-2018, 11:09 PM
(This post was last modified: Oct-16-2018, 12:05 AM by Truman.)
snippsat, your last solution works on my end. Thank you, still don't understand it. I guess that this #--| Setup part can be used in other scripts too. Will save it.
by the way, how to stop music
I tried with but it won't work.
also, is there any point to add Output: time.sleep()
if I want to listen to music for some time? Would try it myself but first have to figure out how to stop music.
Posts: 7,324
Threads: 123
Joined: Sep 2016
Oct-16-2018, 01:06 PM
(This post was last modified: Oct-16-2018, 01:06 PM by snippsat.)
(Oct-15-2018, 11:09 PM)Truman Wrote: I guess that this #--| Setup part can be used in other scripts too. Will save it. Yes can use it for all when work parse or automation ,just comment out --headless then it work without.
Example search and show images of cars.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.keys import Keys
import time
options = Options()
caps = webdriver.DesiredCapabilities().FIREFOX
caps[ "marionette" ] = True
browser = webdriver.Firefox(firefox_options = options, capabilities = caps, executable_path = r "geckodriver.exe" )
input_field = browser.find_elements_by_css_selector( '#search_form_input_homepage' )
input_field[ 0 ].send_keys( 'car' + Keys.RETURN)
time.sleep( 3 )
images_link = browser.find_elements_by_link_text( 'Images' )
images_link[ 0 ].click()
time.sleep( 5 )
browser.quit()
|
Quote:if I want to listen to music for some time? Would try it myself but first have to figure out how to stop music.
No time.sleep() is not for that,you have to push start and stop button on web-site that has music.
Also you find button eg bye CSS selector,then call click() on eg stop button.
Posts: 404
Threads: 94
Joined: Dec 2017
Oct-16-2018, 10:35 PM
(This post was last modified: Oct-16-2018, 10:57 PM by Truman.)
Thank you. The next step for this car script is to download these images. Will get back to that later when I finish the script I started. Remember that there is a chapter in Automate the boring stuff with Python about that stuff.
regarding push and stop button I tried this but it won't stop the music.
1 |
browser.find_element_by_css_selector( '#playbutton playing' )[ 0 ].click()
|
'playbutton playing' is what I see when the music starts.
|