Skip to content

Instantly share code, notes, and snippets.

@haradreborn
Last active December 13, 2017 22:34
Show Gist options
  • Save haradreborn/3ddb75659a50e4ae5f3f to your computer and use it in GitHub Desktop.
Save haradreborn/3ddb75659a50e4ae5f3f to your computer and use it in GitHub Desktop.
Chromedriver examples with locators
import os
from selenium import webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
chromedriver = "D:\\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
driver.get("https://ww.google.com")
#id
driver.find_element_by_id("id").click()
#waiting
ui.WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "id")))
#sendkeys
driver.find_element_by_id("id").send_keys("test")
#link text
driver.find_element_by_link_text("text").click()
#xpath
driver.find_element_by_xpath("//*[contains(text(), 'text')]").click()
driver.find_element(By.XPATH, '//button/span[text()="text"]').click()
#hardcode wait
time.sleep(5)
#options list loop
#class
for option in driver.find_elements_by_class_name('x-boundlist-item'):
print option.text
if option.text == 'Russian Federation':
print('DEBUG: found')
option.click()
break
#name
driver.find_element_by_class_name('text').click()
#checkboxes
checkboxes = driver.find_elements_by_xpath("//input[@name='index3']")
for checkbox in checkboxes:
checkbox.click()
driver.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment