简体   繁体   中英

Why am I not able to open google.com using Selenium?

This project I'm working on is the same project I was working on when I asked this StackOverflow question: I'm getting a name error when running Selenium-based code . Again, both the questions are related to Selenium. However, this time I am not getting any errors but the website I want to be opened isn't opening. And I'm sure my chrome_directory is right. Idk why Google.com isn't opening. This is my code:

from selenium import webdriver
  class Infow():

def __init__(self):
    self.driver = webdriver.Chrome(executable_path='C:\Program Files (x86)\Chrome Driver@C drive\ChromeDriverExtracted\chromedriver_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")

This is the text I see in the terminal

      "D:\Rocland\Selenium Pycharm\Scripts\python.exe" "D:/PycharmProjects/Selenium 
      Pycharm/Selenium for trevor.py"
      Process finished with exit code 0

My apologies if this question is senseless. I'm a Selenium beginner and all the Stack I saw don't answer my problem. (I'm not sure if this will help but I'm using PyCharm on Win10) I hope I made my points clear and anyone's help will be gladly accepted. Thanks in advance!

You have indentation problem in the second row: class Infow()
Try this and then debug by yourself.

from selenium import webdriver


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

Class should look like this:

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

Check the example based on your case. I use Linux, so my executable_path differs from your. Yours seems to be correct because you do not have any errors:

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()

Furthermore, I've implemented in with unittest . This should answer all your questions:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM