为什么我不能使用Selenium打开google.com



我正在处理的这个项目与我问StackOverflow问题时正在处理的项目相同:I';我在运行基于Selenium的代码时遇到了一个名称错误。同样,这两个问题都与硒有关。然而,这一次我没有收到任何错误,但我想打开的网站没有打开。我确信我的chrome_directory是正确的。我知道为什么谷歌没有开通。这是我的代码:

from selenium import webdriver
class Infow():
def __init__(self):
self.driver = webdriver.Chrome(executable_path='C:Program Files (x86)Chrome Driver@C driveChromeDriverExtractedchromedriver_win32 (2)chromedriver.exe')
def get_infow(self, query):
self.query = query
self.driver.get(url="https://www.google.com/")
assist.Infow()
assist.get_infow("exe")

这是我在终端中看到的文本

"D:RoclandSelenium PycharmScriptspython.exe" "D:/PycharmProjects/Selenium 
Pycharm/Selenium for trevor.py"
Process finished with exit code 0

如果这个问题毫无意义,我深表歉意。我是硒的初学者我看到的所有Stack都没有回答我的问题。(我不确定这是否有帮助,但我在Win10上使用PyCharm(我希望我把我的观点讲清楚了,任何人的帮助都会被欣然接受。提前感谢!

第二行出现缩进问题:class Infow()
请尝试此操作,然后自行调试。

from selenium import webdriver

def __init__(self):
self.driver = webdriver.Chrome(executable_path='your path')
self.driver.get(url="https://www.google.com/")

类应该是这样的:

class Testing:
def __init__(self, name=None, number=None):
self.name = name
self.number = number

根据您的案例检查示例。我使用Linux,所以我的executable_path与您的不同。你的似乎是正确的,因为你没有任何错误:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')
driver.get("https://www.google.com/")
assert "Google" in driver.title
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".gLFyf.gsfi")))
input_field = driver.find_element_by_css_selector(".gLFyf.gsfi")
input_field.send_keys("Why are people so mad?")
input_field.send_keys(Keys.RETURN)
wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".yuRUbf")))
results = driver.find_elements_by_css_selector(".yuRUbf>a>h3")
for result in results:
print(result.text)
driver.close()
driver.quit()

此外,我已经在中实现了单元测试。这应该可以回答您的所有问题:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import unittest

class SiteTest(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')
def test(self):
driver = self.driver
driver.get('https://google.com/')
driver.get("https://www.google.com/")
assert "Google" in driver.title
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".gLFyf.gsfi")))
input_field = driver.find_element_by_css_selector(".gLFyf.gsfi")
input_field.send_keys("Why are people so mad?")
input_field.send_keys(Keys.RETURN)
wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".yuRUbf")))
results = driver.find_elements_by_css_selector(".yuRUbf>a>h3")
for result in results:
print(result.text)
def tearDown(self):
self.driver.close()
self.driver.quit()

if __name__ == "__main__":
unittest.main()

相关内容

  • 没有找到相关文章

最新更新