I am trying to make an auto yt searcher and this is as far as I got. I don't really understand on how to user selenium so I am using web-browser what's currently not happening is it is not opening or searching on the search bar (I used the XPath)or does anyone know a tutorial for selenium
import webbrowser
import time
url = 'http://youtube.com'
chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
webbrowser.get(chrome_path).open(url)
LastName = "nice"
time.sleep(2)
last = webbrowser.open('//*[#id="search"]')
webbrowser.get(LastName)
You can't use the actual Google Chrome Browser.
Instead you have to use ChromeDriver that works the same as the standard Chrome but you can use it with selenium.
You can download it here.
Once installed specify the correct path of chromedriver.
Python webbrowser module is not intended for interaction with UI objects. The purpose is just to open browser in order to display arbitrary web document from your python code.
If you check this module page you will know that webbrowser.open(...) takes the URL as a parameter. It cannot work with xpath or other types of selectors. It cannot send other types of commands to browser except of "open that page".
So you have to deal with webdriver.
I would highly recomend to use selenium, because it is not very hard to learn and to integrate.
from selenium import webdriver
api = webdriver.Firefox()
api.get("https://www.youtube.com")
elem = api.find_elements_by_id("search")[1]
elem.send_keys("Your Search")
elem.submit()
Here you only have to change search and it works. Just download firefox and the geckodriver put the geckodriver in the same directory as the script and tadah it should work.
Related
I am writing a python script which includes opening up an URL and do some activity on it. I am facing an issue when i execute below code then Firefox browser starts but it is not able to browse URL. What could be wrong here..?
I also tried to add proxy exception but that isn't solve issue.
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('WEBSITE_URL')
So, Pls suggest what is wrong here.
I keep getting this error:
https://sites.google.com/a/chromium.org/chromedriver/help/chromedriver-crashes
I get it when running the command:
python Web.py
However when I go into the file and run the lines 1 by 1, I don't get the error. However I always get the error when the Web.py file has finished. When I run the lines 1 by 1, it's very basic things but i feel like I"m not ending my script correctly.
import selenium
from selenium.webdriver.common.keys import Keys
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('espn.com')
I want the window with espn.com to stay on the screen, not quit when the script has finished.
I'm running this on Python. I can share my setup, maybe that's something I did incorreclty but any help would be appeciated.
You're passing an invalid url.
You need to pass the url like this:
driver.get("http://www.espn.com")
This might work in your browser, but it won't with selenium. Type in "espn.com" in your browser and then copy / paste the url and you'll see that it's actually the above url.
You should also specify the "chromedriver.exe" path.
You are getting this error because you had not installed the chrome driver for selenium on your Machine. Selenium by default provides the driver for Firefox so when you use the webdriver for Firefox, it won't rise any error. To resolve this issue with Chrome you can download the Chrome webdriver from here.
and you can specify the driver as
from selenium import webdriver
d = webdriver.Chrome(executable_path='<your Chrome driver path>')
Adding to what #Pythonista said , it's better if you keep the URL as a raw string than a normal string
driver.get(r'http://www.espn.com')
so that it won't take the slash as an escape sequence in few cases.
Hope it helps.
Try to update chrome and get updated/latest chrome driver, recently chrome made several updates in its driver you can download the last one from the link below:
https://chromedriver.storage.googleapis.com/2.27/chromedriver_win32.zip
Or perhaps "hack" the actual library code for webbrowser or Selenium to do this? I'm looking in the current documentation and not seeing that this is possible, but perhaps you could adjust the actual library code and insert this functionality?
Unfortunately, the following approach with Selenium doesn't work - it only returns the original URL:
import selenium
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://api.instagram.com/oauth/authorize/?client_id=cb0096f08a387355f&redirect_uri=http://pythondev.instadev.com/instagramredirect.html&response_type=code")
print driver.current_url
No, but if you're looking for a module that gives you more control over the browser look into selenium webdriver.
In selenium the function is driver.current_url
as the bob0the0mighty's link shows, you'll need to have a loop that waits for the url to change. try something like:
initialurl="https://api.instagram.com/oauth/authorize/client_id=cb0096f08a387355f&redirect_uri=http://pythondev.instadev.com/instagramredirect.html&response_type=code"
currenturl="https://api.instagram.com/oauth/authorize/?client_id=cb0096f08a387355f&redirect_uri=http://pythondev.instadev.com/instagramredirect.html&response_type=code"
while(true):
if(currenturl != initialurl):
print currenturl
break
The python webbrowser module launches an external process that is different depending on platform and even environment variables on that platform. So I'd say in general "no".
I am working on a project that does online homework automatically.
I am able to login, finding exercises and even filling the form using mechanize.
I discovered that the submit button trigger a javascript function and I searched for the solution. A lot of answers consist of 'simulating the XHR'. But none of them talked about the details.
I don't know if this screen cap helps.
http://i.stack.imgur.com/0g83g.png
Thanks
If you want to evaluate javascript, I'd recommend using Selenium. It will open a browser which you can then send text to it from python.
First, install Selenium: https://pypi.python.org/pypi/selenium
Then download the chrome driver from here: https://code.google.com/p/chromedriver/downloads/list
Put the binary in the same folder as the python script you're writing. (Or add it to the path or whatever, more information here: https://code.google.com/p/selenium/wiki/ChromeDriver)
Afterwards the following example should work:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.send_keys("selenium")
elem.send_keys(Keys.RETURN)
assert "Google" in driver.title
driver.close()
More information here
(The example was also from there)
A xhr is the same as a regular request. Make it the same way and then deal with the response.
I'm not sure how to find this information, I have found a few tutorials so far about using Python with selenium but none have so much as touched on this.. I am able to run some basic test scripts through python that automate selenium but it just shows the browser window for a few seconds and then closes it.. I need to get the browser output into a string / variable (ideally) or at least save it to a file so that python can do other things on it (parse it, etc).. I would appreciate if anyone can point me towards resources on how to do this. Thanks
using Selenium Webdriver and Python, you would simply access the .page_source property to get the source of the current page.
for example, using Firefox() driver:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://www.example.com/')
print(driver.page_source)
driver.quit()
There's a Selenium.getHtmlSource() method in Java, most likely it is also available in Python. It returns the source of the current page as string, so you can do whatever you want with it
Ok, so here is how I ended up doing this, for anyone who needs this in the future..
You have to use firefox for this to work.
1) create a new firefox profile (not necessary but ideal so as to separate this from normal firefox usage), there is plenty of info on how to do this on google, it depends on your OS how you do this
2) get the firefox plugin: https://addons.mozilla.org/en-US/firefox/addon/2704/ (this automatically saves all pages for a given domain name), you need to configure this to save whichever domains you intend on auto-saving.
3) then just start the selenium server to use the profile you created (below is an example for linux)
cd /root/Downloads/selenium-remote-control-1.0.3/selenium-server-1.0.3
java -jar selenium-server.jar -firefoxProfileTemplate /path_to_your_firefox_profile/
Thats it, it will now save all the pages for a given domain name whenever selenium visits them, selenium does create a bunch of garbage pages too so you could just delete these via a simple regex parsing and its up to you, from there how to manipulate the saved pages