所以我正在尝试做一个if语句来弄清楚如何动态设置硒使用的变量的路径。最主要的是我希望语句查看是否安装了驱动程序,然后根据模块中的platform.system()
函数未安装驱动程序platform
则中断。我有以下内容,但我遇到无效的语法问题。我有路径工作并安装在Windows和Linux系统上,所以我知道它们有效。
import selenium
import shutil
import xlsxwriter
import os
import unittest
import requests
import subprocess
import getpass
import platform
import logging
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from datetime import date
# Definitions
# find_elements_by_name
# find_elements_by_xpath
# find_elements_by_link_text
# find_elements_by_partial_link_text
# find_elements_by_tag_name
# find_elements_by_class_name
# find_elements_by_css_selector
# System Variables
date = today.strftime("%m/%d/%Y")
system = platform.system()
today = date.today()
username = getpass.getuser()
# URL Variables
login_url = 'https://www.accuplacer.org/'
redirect_url = 'https://www.accuplacer.org/api/home.html#/'
reports_scheduler_url = 'https://www.accuplacer.org/api/home.html#/reportScheduler'
custom_reports_url = 'https://www.accuplacer.org/api/home.html#/customReports'
# WebDriver Path for System
if system = ('Windows'):
browser = webdriver.Chrome("C:Program Files (x86)GoogleChromechromedriver.exe")
elif system = ('Linux'):
broswer = webdriver.Chrome("~/Drivers/Google/Chrome/chromedriver_linux64")
elif system = ('Darwin'):
browser = webdriver("~/Drivers/Google/Chrome/chromedriver_mac64")
else:
print("Are you sure you have the Selenium Webdriver for Chrome installed in the correct path?")
continue
# Parent URL
browser.get("https://www.accuplacer.org")
当我尝试在 Linux 或 Windows 中拉出该站点时,我收到以下语法错误:
File "secret_collegeboard_tsi_export.py", line 56
if system = ('Windows'):
^
SyntaxError: invalid syntax
请改用这个:
if system == ('Windows'):
browser = webdriver.Chrome("C:Program Files (x86)GoogleChromechromedriver.exe")
elif system == ('Linux'):
broswer = webdriver.Chrome("~/Drivers/Google/Chrome/chromedriver_linux64")
elif system == ('Darwin'):
browser = webdriver("~/Drivers/Google/Chrome/chromedriver_mac64")
= 是赋值运算符 == 是比较运算符
=用于为variable
赋值。用于equality
检查string
使用
if system is 'Windows':
browser = webdriver.Chrome("C:Program Files (x86)GoogleChromechromedriver.exe")
或
if system == 'Windows':
browser = webdriver.Chrome("C:Program Files (x86)GoogleChromechromedriver.exe")
我实际上很快就想通了。我使用了以下内容,这与@Muzzamil和@testfile提及相似:
# WebDriver Path for System
if platform.system() == ('Windows'):
browser = webdriver.Chrome("C:Program Files (x86)GoogleChromechromedriver.exe")
elif platform.system() == ('Linux'):
browser = webdriver.Chrome(executable_path='/home/rbarrett/Drivers/Google/Chrome/chromedriver_linux64/chromedriver')
elif platform.system() == ('Darwin'):
browser = webdriver(executable_path='~/Drivers/Google/Chrome/chromedriver_mac64/chromedriver')
else:
print("Are you sure you have the Selenium Webdriver installed in the correct path?")