Using PhantomJS through Selenium with Python - WebDriverException error - selenium-webdriver

I am using the following code, as recommended here: Is there a way to use PhantomJS in Python?.
from selenium import webdriver
driver = webdriver.PhantomJS()
driver.set_window_size(1024, 768) # optional
driver.get('https://google.com/')
driver.save_screenshot('screen.png') # save a screenshot to disk
sbtn = driver.find_element_by_css_selector('button.gbqfba')
sbtn.click()
When I try to run it I get the following error:
WebDriverException - "Unable to start phantomjs with ghostdriver."
Apparently, this error can be solved by replacing the 3rd line with:
driver = webdriver.PhantomJS(executable_path='/usr/local/lib/node_modules/phantomjs/lib/phantom/bin/phantomjs')
I'm using Windows7, and the path for me appears to be: "C:\Users\myname\AppData\Roaming\npm\node_modules\phantomjs\bin\phantomjs". I have tried using this and variations of it as the executable_path, but I still get the same error. I have also tried adding the folder location to the path. Nothing has worked. I suspect that I am missing something pretty obvious.

Aha!
It was a slightly different location. The line I was looking for was:
driver = webdriver.PhantomJS(executable_path=r'C:\Users\myname\AppData\Roaming\npm\node_modules\phantomjs\lib\phantom\phantomjs')

Related

Hiding the CMD window of PhantomJS on python with selenium

I am trying to hide the CMD window that appears when launching PhantomJS with python:
from selenium import webdriver
browser = webdriver.PhantomJS()
There appears to be a solution when working with C#, however, I was not able to find anything similar for python. Since the feature for C# was added back in 2014, I assume something similar should exist for python too.
I'm using latest PhantomJS and Python 3.6.2.
This is how it looks like:
For me,
The below code works fine.
driver = webdriver.PhantomJS(
service_args=service_args,
desired_capabilities=caps,
executable_path='phantomjs.exe',
service_log_path=None
)
Here you have to add service_log_path=None for no log file that's why the console window will get hidden.
Still Not solved then
replace code from
(C:\Users\YOUR_USER_NAME\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\selenium\webdriver\common\service.py) file
self.process = subprocess.Popen(cmd, env=self.env, close_fds=platform.system() != 'Windows', stdout=self.log_file, stderr=self.log_file, stdin=PIPE)
to
self.process = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False, creationflags=0x08000000)

Open firefox browser using Selenium 3.5.2 , Python3.6

I had installed python 3.6 and selenium 3.5.2 version,geckodriver.exe . But when i used below code not able to open fire fox
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
driver=webdriver.Firefox("C:\\Users\\Desktop\\Selenium\\seleniumfirefoxdriver\\geckodriver.exe")
driver.set_page_load_timeout(20)
driver.get("www.google.com")`enter code here`
driver.maximize_window()
driver.implicitly_wait(20)
Error shown as
NotADirectory Error:
[Win Error 267] The directory name is invalid: C:\\Users\\Desktop\\Selenium\\selenium firefoxdriver\\geckodriver.exe
Try below code-
driver = webdriver.Firefox(executable_path=r'C:\Users\Desktop\Selenium\seleniumfirefoxdriver\geckodriver.exe')
If there is a space in folder name 'selenium firefoxdriver', rename that folder to 'seleniumfirefoxdriver'
Try single slash and folder name doesn't contain space either use _ or remove space in folder name "selenium firefoxdriver". Make it "selenium_firefoxdriver" or "SeleniumFirefoxdriver".
and then use this code:
driver = webdriver.Firefox(executable_path=r'C:/Users/Desktop/Selenium/selenium_firefoxdriver/geckodriver.exe')
or
driver = webdriver.Firefox(executable_path=r'C:/Users/Desktop/Selenium/SeleniumFirefoxdriver/geckodriver.exe')

browser.get(URL) does not work

I am having an issue with entering text into the address bar nothing happens and I get the error
selenium.common.exceptions.WebDriverException: Message: Can't load the
profile. Profile Dir:
/var/folders/8_/f48lnzrs7r59_h2yb3dkfbh40000gn/T/tmpsGffLD If you
specified a log_file in the FirefoxBinary constructor, check it for
details.
I am able to open the browser but when I uncomment the code browser.get('http://www.google.com') nothing happens and I get the error listed above. I am using Firefox 48, Python 2.7.10 and selenium-2.53.6-py2.7.egg
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.google.com')
Change that line to
browser.url('http://www.google.com')
OR
browser.open('http://www.google.com')

R - error installing caret package

> Error in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()),
> versionCheck = vI[[j]]) : namespace ‘pbkrtest’ 0.4-2 is being
> loaded, but >= 0.4.4 is required
> Error: package or namespace load
> failed for ‘caret’
Caret was working fine until I tried to load Rcpp and it messed everything up.
I searched the answers for a similar problem with caret but the solutions posted did not seem to work on mine. I followed:
install.packages("caret", dependencies = TRUE)
But it did not work.
I would suggest you to check the R version. I updated the version to 3.5.1 and it works perfectly :)
Mac only – use updateR
Similar to installr, updateR is the package to help updating R on Mac OS.
The R code you will need are the following 5 lines:
install.packages('devtools') #assuming it is not already installed
library(devtools)
install_github('andreacirilloac/updateR')
library(updateR)
updateR(admin_password = 'Admin user password')
I have the same problem with my dynlm package. I get the same error. But as R states pbkrtest is required. This package isn't available under R 3.2.3.
However you can download the package online, look a bit further on this site, the question is already asked on stackoverflow and they gave a site where you can find the packages. Then write the following code:
install.packages("...",repos = NULL, type="source")
install.packages("pbkrtest", dependencies = TRUE)
In the first line, here I wrote the 3 dots you need to write the path to the file where you placed the pbkrtest-package.
I also faced the same problem with caret package and I could solve it as the following:
install.packages("lme4", dependencies = TRUE)
library(lme4)
methods(sigma)
install.packages("pbkrtest", dependencies = TRUE)
library(caret)
This worked for me!
Try
install.packages('DEoptimR')
and followed by
install.packages("caret", dependencies = TRUE)
I had the same issues. Both of these commands worked for me.
I faced the same error with caret and none of the above suggestions helped. My R version was 3.2 and current version is 3.4.
I installed the new version and the issue was resolved
I have faced this issue and tried many ways but followed r console about error thrown and started downloading packages one by one. steps noted down what i did for this. y r studio version is "R version 3.4.4"
First install.packages("caret"), install.packages("ggplot2"), install.packages("lattice"), install.packages("lava")..
then load library(ggplot2) then library(lattice) then library(lava) and then finally library(caret).
What i believe that caret have these packages dependency so once caret is installed ideally 'install.packages('caret', dependencies = TRUE)' should work but it was not working in my R version so i did the step as given above and it worked for me.
hope it may work if someone get this issue
I did try all the above mentioned ways to install and activate caret but none worked for me.
finally what I did was to go to my drive location where R libraries are located. I removed a folder called "caret" and then in R studio I run "remove.packeges("caret")" to remove the caret package.
then I reinstalled the package.
install.packages("caret")
library(caret)
it worked for me.
I'm in R 3.6.1 and got the same error today.
I used this code:
install.packages("caret",dep = TRUE)
install.packages("ggplot2")
install.packages("lattice")
install.packages("lava")
install.packages("purrr")
library(ggplot2)
library(lattice)
library(lava)
library(purrr)
library(caret)
And now it works fine for me. It's all about dependencies that you should install with caret.
Use this:
install.packages(c("ggplot2", "lattice", "lava", "purrr", "caret"))
library(c("ggplot2", "lattice", "lava", "purrr", "caret"))
If that doesn't work, create a folder (R_LIB in this case) on your computer (documents in this case) and include the folder location as follow:
install.packages(c("ggplot2", "lattice", "lava", "purrr", "caret"), lib = "C:/documents/R_LIB")
library(c("ggplot2", "lattice", "lava", "purrr", "caret"), lib = "C:/documents/R_LIB")

Could not start Selenium session: You may not start more than one session at a time

this is a configuration error , when trying to configure selenium with eclipse , what i read in a couple of websites is that , adding a dependency to maven would do , but nothing worked , i have been spending a lot of hours/day , even modified the system property , to add firefox profile into it , but nothing worked . looks like i am the first one on this earth to get this error
thankyou
pasting a stack trace , for your reference
java.lang.RuntimeException: Could not start Selenium session: You may not start more than one session at a time
at com.thoughtworks.selenium.DefaultSelenium.start(DefaultSelenium.java:90)
at TestAddVisitor.setUp(TestAddVisitor.java:36)
at junit.framework.TestCase.runBare(TestCase.java:132)
at com.thoughtworks.selenium.SeleneseTestCase.runBare(SeleneseTestCase.java:228)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: com.thoughtworks.selenium.SeleniumException: You may not start more than one session at a time
at org.openqa.selenium.WebDriverCommandProcessor.start(WebDriverCommandProcessor.java:217)
at org.openqa.selenium.WebDriverCommandProcessor.start(WebDriverCommandProcessor.java:208)
at com.thoughtworks.selenium.DefaultSelenium.start(DefaultSelenium.java:82)
... 16 more
I guess this is not a configuration error. If my understanding is correct you are are creating WebDriver object and then creating DefaultSelenium / Selenium object with it and then starting the selenium session.
then -- don't use DefaultSelenium.start() method.
Code should be like this:
DefaultSelenium ds = new DefaultSelenium(
new WebDriverCommandProcessor("baseURL",new FirefoxDriver()));
// ds.start(); -- do not do this
ds.open("/");
This might help you.
Did you take a look at http://seleniumhq.org/docs/03_webdriver.html#webdriver-backed-selenium-rc? sudarsan in the previous answer is right: don't do selenium.start.
If you wonder that the JUnit Code from the Selenium IDE throughs this exception, see http://osdir.com/ml/selenium-users/2011-07/msg02107.html

Resources