我正在尝试在AWS EC2 Ubuntu机器的chrome浏览器上执行Selenium脚本。chromedriver放在"/usr/local/bin"
中,我还通过运行下面的命令使该文件可执行:
sudo chmod +x /usr/local/bin/chromedriver
请注意,我使用docker作为执行器,GitLab Runner安装在上述AWS EC2 Ubuntu机器上。
下面是设置chromedriver路径的Selenium代码:
File file = new File("/usr/local/bin/chromedriver");
System.out.println("Does file exists ? " + file.exists());
System.out.println("Check whether file is executable : " + file.canExecute());
System.out.println("Check whether file is readable : " + file.canRead());
System.out.println("Check whether file is writable : " + file.canWrite());
// All the above 4 statements throws false..
System.setProperty("webdriver.chrome.driver","/usr/local/bin/chromedriver");
下面是chrome-driver, google-chrome, java, docker和gitlab-runner的路径:
ubuntu@ip-172-31-29-94:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
ubuntu@ip-172-31-29-94:~$ whereis chromedriver
chromedriver: /usr/local/bin/chromedriver
ubuntu@ip-172-31-29-94:~$ whereis google-chrome
google-chrome: /usr/bin/google-chrome /usr/share/man/man1/google-chrome.1.gz
ubuntu@ip-172-31-29-94:~$ whereis java
java: /usr/bin/java /usr/share/java /usr/share/man/man1/java.1.gz
ubuntu@ip-172-31-29-94:~$ google-chrome -version
Google Chrome 93.0.4577.82
ubuntu@ip-172-31-29-94:~$ chromedriver -version
ChromeDriver 93.0.4577.63 (ff5c0da2ec0adeaed5550e6c7e98417dac77d98a-refs/branch-heads/4577@{#1135})
ubuntu@ip-172-31-29-94:~$ whereis docker
docker: /usr/bin/docker /etc/docker /usr/libexec/docker /usr/share/man/man1/docker.1.gz
ubuntu@ip-172-31-29-94:~$ whereis gitlab-runner
gitlab-runner: /usr/bin/gitlab-runner /usr/lib/gitlab-runner /etc/gitlab-runner /usr/share/gitlab-runner
ubuntu@ip-172-31-29-94:~$ ls
chromedriver_linux64.zip gitlab-runner_amd64.deb google-chrome-stable_current_amd64.deb
ubuntu@ip-172-31-29-94:~$ pwd
/home/ubuntu
ubuntu@ip-172-31-29-94:/$ pwd
/
ubuntu@ip-172-31-29-94:/$ ls
bin dev home lib32 libx32 media opt root sbin srv tmp var
boot etc lib lib64 lost+found mnt proc run snap sys usr
下面是控制台日志:
30652 [ERROR] testing.TC_003.bmeth[org.testng.TestRunner@64161330, org.testng.xml.XmlTest@b878032e, public void testing.TC_003.zabc() throws java.lang.InterruptedException, [Ljava.lang.Object;@609ad06b, [TestResult name={null} status=CREATED method=TC_003.zabc()[pri:0, instance:testing.TC_003@432038ec] output={null}]](0) Time elapsed: 1.759 s <<< FAILURE!
java.lang.IllegalStateException: The driver executable must exist: /usr/local/bin/chromedriver
at testing.TC_003.bmeth(TC_003.java:42)
下面是gitlab yml文件:
image: maven:latest
stages:
- test
variables:
# This will suppress any download for dependencies and plugins or upload messages which would clutter the console log.
# `showDateTime` will show the passed time in milliseconds. You need to specify `--batch-mode` to make this work.
MAVEN_OPTS: "-Dhttps.protocols=TLSv1.2 -Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true"
# Cache downloaded dependencies and plugins between builds.
# To keep cache across branches add 'key: "$CI_JOB_NAME"'
cache:
key: $CI_COMMIT_REF_SLUG
paths:
- .m2/repository
test job:
stage: test
tags:
- docker-linux
script:
- echo "Testing Job Triggered"
- mvn $MAVEN_OPTS clean test
- echo "Testing Job Finished"
artifacts:
when: always
paths:
- target/surefire-reports/*
任何帮助都是感激的。
为了让Chromedriver在Linux上工作,你必须安装Chrome二进制文件
# Install Chrome.
sudo curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add
sudo echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list
sudo apt-get -y update
sudo apt-get -y install google-chrome-stable
安装Xvfb(X虚拟framebuffer)是一个类unix操作系统的内存显示服务器
sudo apt-get install -y unzip xvfb libxi6 libgconf-2-4
下载Selenium Server:
wget https://selenium-release.storage.googleapis.com/3.13/selenium-server-standalone-3.13.0.jar
通过Selenium Server运行Chrome:
xvfb-run java -Dwebdriver.chrome.driver=/usr/bin/chromedriver -jar selenium-server-standalone.jar
然后你可以运行这个python脚本来使用selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
CHROMEDRIVER_PATH = '/usr/local/bin/chromedriver'
WINDOW_SIZE = "1920,1080"
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=%s" % WINDOW_SIZE)
chrome_options.add_argument('--no-sandbox')
driver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH,
chrome_options=chrome_options
)
driver.get("https://www.google.com")
print(driver.title)
driver.close()
如果没有帮助,请尝试查看您提供的路径。当无法找到chrome二进制文件时,就会发生此异常。所以如果驱动程序在那个文件夹中,那么问题可能存在于path.
更新:
检查chromedriver是否是可执行文件的名称或文件夹名称。您应该在您的路径中提供可执行文件的名称。
所以如果在/usr/local/bin/chromedriver
的chromedriver目录下的可执行文件,你的代码应该是:
System.setProperty("webdriver.chrome.driver","/usr/local/bin/chromedriver/chromedriver");
上面的代码中,第一个chromedriver
是文件夹的名称,第二个是可执行文件的名称。