Why am I not able to open google.com using Selenium?
You have indentation problem in the second row: class Infow()
Try this and then debug by yourself.
from selenium import webdriverdef __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 webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECdriver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')driver.get("https://www.google.com/")assert "Google" in driver.titlewait = 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 webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECimport unittestclass 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()