I'm trying to code an automation function which opens the entered URL. The code works if I'm not working with Chrome browser; but doesn't work with Chrome browser: it fails to detect my Chrome Browser and returns the error "could not find runnable browser".
Code:
import webbrowser
def visitwebsite():
url = input('Enter url: ')
webbrowser.get('chrome').open_new_tab(url)
Tried already 'chrome' - 'google-chrome' and redirecting it directly to the path. Maybe i did it wrong
The code is simple. You need to install selenium for this.
from selenium import webdriver
driver = webdriver.Chrome(executable_path='path to chromedriver.exe')
driver.get('https://www.google.com)
Related
so I have started working on selenium, and this is my first time working with it, the code provided below opens chrome but doesnt open the url mentioned in the .get function.
from selenium import webdriver
import time
# open chrome and access the page
driver = webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chrome.exe")
driver.get('https://dex.onxrp.com/?project=XREEFS')
time.sleep(5)
driver.close()
I have installed the selenium pip library, do i need something else as well for it to load the link?
Using selenium3 the default argument which can be passed within webdriver.Chrome() is the value of the KEY executable_path i.e. the logical/absolute path of the ChromeDriver executable but not of google-chrome executable.
Solution
Ideally, your line of code will be:
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe')
I want to know that is there any possible way to get the current working URL in Google Chrome.
I tried using selenium, pywinauto libraries in python, but couldn't get the expected output.
In selenium:
import selenium
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager.install())
print(driver.current_url())
but it gives a syntax error
from webdriver_manager.driver import ChromeDriver
self.auth_header = {'Authorization': f'token {self._os_token}'}
^
SyntaxError: invalid syntax
My working platform is 64bit windows10, python 3.5. Hope will get the answer. Thanks
current_url
current_url is a property which gets the URL of the current page.
Solution
So instead of:
print(driver.current_url())
You need to:
print(driver.current_url)
This usecase
To extract the current_url first you need to invoke the url through get and then retrieve the url as follows:
get("https://www.google.com/")
print(driver.current_url)
Though the default browser is IE or Firefox, when I run the program, I want the website to be opened in chrome.
At first, I tried this out--
import webbrowser
import os
path = "C:\Program Files (x86)\Google\Chrome\Application %s"
webbrowser.get(path).open("youtube.com")
But the webpage opened in Ie as my default browser is IE.
Then I tried the below code-
from selenium import webdriver
browser = webdriver.Chrome()
browser.open('https://directory.corp.intranet/cmsviewer/login.html')
But received many errors. Please help me out!!!
Hello and welcome to Stack overflow
try to do this :
import webbrowser
chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
webbrowser.get(chrome_path).open('http://docs.python.org/')
good luck
Try maybe the following:
import webbrowser
webbrowser.get('chrome').open("youtube.com")
As it seems that chrome is already registered by default:
https://docs.python.org/3.8/library/webbrowser.html#webbrowser.register
Your selenium code is very likely not working due to the lack web extensions like .net, .com,.org, .aspx etc. So your selenium code should work if it looked like
from selenium import webdriver
browser = webdriver.Chrome()
browser.open('https://directory.corp.intranet.aspx')
After running pip selenium and downloading chromedriver.exe to C:/chromedriver/chromedriver.exe directory. Running my program results in Chrome not being able to open the URL and the following message prompts in a dialog-box:
Chromedriver stopped working
This is my attempt at testing that the source-page could be accessed.
import requests
from selenium import webdriver
Base_url = "https:/www.facebook.com"
driver = webdriver.Chrome(r'C:/chromedriver/chromedriver.exe')
driver.get(Base_url)
print (driver.page_source)
Can someone help me sort this out?
Thank you.
To open Chrome Browser using chromedriver binary you need to pass the argument executable_path along with the absolute path of the chromedriver binary and invoke the proper url as follows :
from selenium import webdriver
Base_url = "https://www.facebook.com/"
driver = webdriver.Chrome(executable_path=r'C:/chromedriver/chromedriver.exe')
driver.get(Base_url)
print (driver.page_source)
I am having a strange issue with PhantomJS or may be I am newbie. I am trying to login on NewEgg.com via Selenium by using PhantomJS. I am using Python for it. Issue is, when I use Firefox as a driver it works well but as soon as I set PhantomJS as a driver it does not go to next page hence give message:
Exception Message: u'{"errorMessage":"Unable to find element with id \'UserName\'","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"89","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:55372","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\\"using\\": \\"id\\", \\"sessionId\\": \\"aaff4c40-6aaa-11e4-9cb1-7b8841e74090\\", \\"value\\": \\"UserName\\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/aaff4c40-6aaa-11e4-9cb1-7b8841e74090/element"}}' ; Screenshot: available via screen
The reason I found after taking screenshot that phantom could not navigate the page and script got finished. How do I sort this out? Code Snippet I tried given below:
import requests
from bs4 import BeautifulSoup
from time import sleep
from selenium import webdriver
import datetime
my_username = "user#mail.com"
my_password = "password"
driver = webdriver.PhantomJS('/Setups/phantomjs-1.9.7-macosx/bin/phantomjs')
firefox_profile = webdriver.FirefoxProfile()
#firefox_profile.set_preference('permissions.default.stylesheet', 2)
firefox_profile.set_preference('permissions.default.image', 2)
firefox_profile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', 'false')
#driver = webdriver.Firefox(firefox_profile)
driver.set_window_size(1120, 550)
driver.get('http://newegg.com')
driver.find_element_by_link_text('Log in or Register').click()
driver.save_screenshot('screen.png')
I even put sleep but it is not making any difference.
I experienced this with PhantomJS when the content type of the second page is not correct. A normal browser would just interpret the content dynamically, but Phantom just dies, silently.