本文整理汇总了Python中selenium.webdriver.common.by.By.CSS_SELECTOR属性的典型用法代码示例。如果您正苦于以下问题:Python By.CSS_SELECTOR属性的具体用法?Python By.CSS_SELECTOR怎么用?Python By.CSS_SELECTOR使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类selenium.webdriver.common.by.By
的用法示例。
在下文中一共展示了By.CSS_SELECTOR属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: index_page
# 需要导入模块: from selenium.webdriver.common.by import By [as 别名]
# 或者: from selenium.webdriver.common.by.By import CSS_SELECTOR [as 别名]
def index_page(page):
try:
print('正在爬取第: %s 页' % page)
wait.until(
EC.presence_of_element_located((By.ID, "dt_1")))
# 判断是否是第1页,如果大于1就输入跳转,否则等待加载完成。
if page > 1:
# 确定页数输入框
input = wait.until(EC.presence_of_element_located(
(By.XPATH, '//*[@id="PageContgopage"]')))
input.click()
input.clear()
input.send_keys(page)
submit = wait.until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, '#PageCont > a.btn_link')))
submit.click()
time.sleep(2)
# 确认成功跳转到输入框中的指定页
wait.until(EC.text_to_be_present_in_element(
(By.CSS_SELECTOR, '#PageCont > span.at'), str(page)))
except Exception:
return None
示例2: _find_web_element
# 需要导入模块: from selenium.webdriver.common.by import By [as 别名]
# 或者: from selenium.webdriver.common.by.By import CSS_SELECTOR [as 别名]
def _find_web_element(self):
"""Find WebElement using element locator and save it in _web_element attribute"""
if not self._web_element or not self.config.getboolean_optional('Driver', 'save_web_element'):
# If the element is encapsulated we use the shadowroot tag in yaml (eg. Shadowroot: root_element_name)
if self.shadowroot:
if self.locator[0] != By.CSS_SELECTOR:
raise Exception('Locator type should be CSS_SELECTOR using shadowroot but found: '
'%s' % self.locator[0])
# querySelector only support CSS SELECTOR locator
self._web_element = self.driver.execute_script('return document.querySelector("%s").shadowRoot.'
'querySelector("%s")' % (self.shadowroot,
self.locator[1]))
else:
# Element will be finded from parent element or from driver
base = self.utils.get_web_element(self.parent) if self.parent else self.driver
# Find elements and get the correct index or find a single element
self._web_element = base.find_elements(*self.locator)[self.order] if self.order else base.find_element(
*self.locator)
示例3: get_web_value
# 需要导入模块: from selenium.webdriver.common.by import By [as 别名]
# 或者: from selenium.webdriver.common.by.By import CSS_SELECTOR [as 别名]
def get_web_value(self, element):
"""
Gets the informations of field based in the ID
"""
if element.tag_name == "div":
element_children = element.find_element(By.CSS_SELECTOR, "div > * ")
if element_children is not None:
element = element_children
if element.tag_name == "label":
web_value = element.get_attribute("text")
elif element.tag_name == "select":
current_select = int(element.get_attribute('value'))
selected_element = element.find_elements(By.CSS_SELECTOR, "option")[current_select]
web_value = selected_element.text
else:
web_value = element.get_attribute("value")
return web_value
示例4: open
# 需要导入模块: from selenium.webdriver.common.by import By [as 别名]
# 或者: from selenium.webdriver.common.by.By import CSS_SELECTOR [as 别名]
def open(self):
try:
self.worker.first(By.CSS_SELECTOR, '#TABLE_DATA_fileList')
except NoSuchElementException:
self.print_letter_status('Opening table...', '')
self.worker.get('/mng/action/home.do?mode=ajax')
# Open Alma configuration
self.worker.wait_for_and_click(
By.CSS_SELECTOR, '#ALMA_MENU_TOP_NAV_configuration')
# text() = "General"
self.worker.click(By.XPATH, '//*[@href="#CONF_MENU6"]')
self.worker.click(By.XPATH, '//*[text() = "Customize Letters"]')
self.worker.wait_for(By.CSS_SELECTOR, '#TABLE_DATA_fileList')
return self
示例5: read
# 需要导入模块: from selenium.webdriver.common.by import By [as 别名]
# 或者: from selenium.webdriver.common.by.By import CSS_SELECTOR [as 别名]
def read(self):
# Identify the indices of the column headers we're interested in
elems = self.worker.all(By.CSS_SELECTOR, '#TABLE_DATA_fileList tr > th')
column_headers = [el.get_attribute('id') for el in elems]
filename_col = column_headers.index('SELENIUM_ID_fileList_HEADER_cfgFilefilename') + 1
updatedate_col = column_headers.index('SELENIUM_ID_fileList_HEADER_updateDate') + 1
# Read the filename column
elems = self.worker.all(By.CSS_SELECTOR,
'#TABLE_DATA_fileList tr > td:nth-child(%d) > a' % filename_col)
self.filenames = [el.text.replace('../', '') for el in elems]
# Read the modification date column
elems = self.worker.all(By.CSS_SELECTOR,
'#TABLE_DATA_fileList tr > td:nth-child(%d) > span' % updatedate_col)
self.update_dates = [el.text for el in elems]
# return [{x[0]:2 {'modified': x[1], 'index': n}} for n, x in enumerate(zip(filenames, update_dates))]
示例6: regular_login
# 需要导入模块: from selenium.webdriver.common.by import By [as 别名]
# 或者: from selenium.webdriver.common.by.By import CSS_SELECTOR [as 别名]
def regular_login(url, course_folder, driver):
driver.get("https://www.lynda.com/signin/") # launch lynda.com/signin
# enter username
email = driver.find_element_by_css_selector("#email-address")
email.clear()
email.send_keys(read.username)
driver.find_element_by_css_selector('#username-submit').click()
print('\nusername successfully entered ....')
# wait for password field to appear
WebDriverWait(driver, 15).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "#password-input")))
# enter password
passwrd = driver.find_element_by_css_selector('#password-input')
passwrd.send_keys(read.password)
driver.find_element_by_css_selector('#password-submit').click()
print('password successfully entered ....')
示例7: __init__
# 需要导入模块: from selenium.webdriver.common.by import By [as 别名]
# 或者: from selenium.webdriver.common.by.By import CSS_SELECTOR [as 别名]
def __init__(self, wait, screenshot=None, session=None):
chrome_options = Options()
if session:
chrome_options.add_argument("--user-data-dir={}".format(session))
self.browser = webdriver.Chrome(options=chrome_options) # we are using chrome as our webbrowser
else:
self.browser = webdriver.Chrome()
self.browser.get("https://web.whatsapp.com/")
# emoji.json is a json file which contains all the emojis
with open("emoji.json") as emojies:
self.emoji = json.load(emojies) # This will load the emojies present in the json file into the dict
WebDriverWait(self.browser,wait).until(EC.presence_of_element_located(
(By.CSS_SELECTOR, '._3FRCZ')))
if screenshot is not None:
self.browser.save_screenshot(screenshot) # This will save the screenshot to the specified file location
# This method is used to send the message to the individual person or a group
# will return true if the message has been sent, false else
示例8: get_invite_link_for_group
# 需要导入模块: from selenium.webdriver.common.by import By [as 别名]
# 或者: from selenium.webdriver.common.by.By import CSS_SELECTOR [as 别名]
def get_invite_link_for_group(self, groupname):
search = self.browser.find_element_by_css_selector("._3FRCZ")
search.send_keys(groupname+Keys.ENTER)
self.browser.find_element_by_css_selector("#main > header > div._5SiUq > div._16vzP > div > span").click()
try:
#time.sleep(3)
WebDriverWait(self.browser, self.timeout).until(EC.presence_of_element_located(
(By.CSS_SELECTOR, "#app > div > div > div.MZIyP > div._3q4NP._2yeJ5 > span > div > span > div > div > div > div:nth-child(5) > div:nth-child(3) > div._3j7s9 > div > div")))
invite_link = self.browser.find_element_by_css_selector("#app > div > div > div.MZIyP > div._3q4NP._2yeJ5 > span > div > span > div > div > div > div:nth-child(5) > div:nth-child(3) > div._3j7s9 > div > div")
invite_link.click()
WebDriverWait(self.browser, self.timeout).until(EC.presence_of_element_located(
(By.ID, "group-invite-link-anchor")))
link = self.browser.find_element_by_id("group-invite-link-anchor")
return link.text
except:
print("Cannot get the link")
# This method is used to exit a group
示例9: send_anon_message
# 需要导入模块: from selenium.webdriver.common.by import By [as 别名]
# 或者: from selenium.webdriver.common.by.By import CSS_SELECTOR [as 别名]
def send_anon_message(self, phone, text):
payload = urlencode({"phone": phone, "text": text, "source": "", "data": ""})
self.browser.get("https://api.whatsapp.com/send?"+payload)
try:
Alert(self.browser).accept()
except:
print("No alert Found")
WebDriverWait(self.browser, self.timeout).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#action-button")))
send_message = self.browser.find_element_by_css_selector("#action-button")
send_message.click()
confirm = WebDriverWait(self.browser, self.timeout+5).until(EC.presence_of_element_located(
(By.XPATH, "/html/body/div/div/div/div[4]/div/footer/div[1]/div[2]/div/div[2]")))
confirm.clear()
confirm.send_keys(text+Keys.ENTER)
# Check if the message is present in an user chat
示例10: load_initial
# 需要导入模块: from selenium.webdriver.common.by import By [as 别名]
# 或者: from selenium.webdriver.common.by.By import CSS_SELECTOR [as 别名]
def load_initial(self, company):
url = 'https://www.linkedin.com/company/{}'.format(company)
self.driver.get(url)
try:
myElem = WebDriverWait(self.driver, self.timeout).until(AnyEC(
EC.presence_of_element_located(
(By.CSS_SELECTOR, '.organization-outlet')),
EC.presence_of_element_located(
(By.CSS_SELECTOR, '.error-container'))
))
except TimeoutException as e:
raise ValueError(
"""Took too long to load company. Common problems/solutions:
1. Invalid LI_AT value: ensure that yours is correct (they
update frequently)
2. Slow Internet: increase the timeout parameter in the Scraper constructor""")
try:
self.driver.find_element_by_css_selector('.organization-outlet')
except:
raise ValueError(
'Company Unavailable: Company link does not match any companies on LinkedIn')
示例11: get_first_connections
# 需要导入模块: from selenium.webdriver.common.by import By [as 别名]
# 或者: from selenium.webdriver.common.by.By import CSS_SELECTOR [as 别名]
def get_first_connections(self):
try:
see_connections_link = WebDriverWait(self.driver, self.timeout).until(EC.presence_of_element_located((
By.CSS_SELECTOR,
'.pv-top-card-v2-section__link--connections'
)))
except TimeoutException as e:
print("""Took too long to load connections link. This usually indicates you were trying to
scrape the connections of someone you aren't connected to.""")
return []
see_connections_link.click()
try:
self.configure_connection_type()
except TimeoutException:
return []
all_conns = []
示例12: log_in_user
# 需要导入模块: from selenium.webdriver.common.by import By [as 别名]
# 或者: from selenium.webdriver.common.by.By import CSS_SELECTOR [as 别名]
def log_in_user(self):
driver = self.webdriver
self.log('You need to login to access this profile.',
'Redirecting you to the login page in the browser.',
level=logging.WARN)
driver.get(self.get_url('accounts/login/'))
# Wait until user has been successfully logged in and redirceted
# to his/her feed.
WebDriverWait(driver, 60).until(
expected_conditions.presence_of_element_located(
(By.CSS_SELECTOR, '.-cx-PRIVATE-FeedPage__feed'),
)
)
self.log('User successfully logged in.', level=logging.INFO)
self.set_num_posts() # Have to set this again
driver.get(self.profile_url)
示例13: init_page_elements
# 需要导入模块: from selenium.webdriver.common.by import By [as 别名]
# 或者: from selenium.webdriver.common.by.By import CSS_SELECTOR [as 别名]
def init_page_elements(self):
self.language = PageElement(By.ID, 'language')
self.email = PageElement(By.ID, 'email', mock_element)
self.address = PageElement(By.ID, 'address', (By.ID, 'parent'))
self.address_shadowroot = PageElement(By.CSS_SELECTOR, '#address', shadowroot='shadowroot_css')
self.address_shadowroot_by_id = PageElement(By.ID, 'address', shadowroot='shadowroot_css')
示例14: test_get_web_element_shadowroot_wrong_locator
# 需要导入模块: from selenium.webdriver.common.by import By [as 别名]
# 或者: from selenium.webdriver.common.by.By import CSS_SELECTOR [as 别名]
def test_get_web_element_shadowroot_wrong_locator(driver_wrapper):
with pytest.raises(Exception) as excinfo:
RegisterPageObject(driver_wrapper).address_shadowroot_by_id.web_element
assert "Locator type should be CSS_SELECTOR using shadowroot but found: id" in str(excinfo.value)
mock_element.find_element.assert_not_called()
示例15: findElement
# 需要导入模块: from selenium.webdriver.common.by import By [as 别名]
# 或者: from selenium.webdriver.common.by.By import CSS_SELECTOR [as 别名]
def findElement(parent, css_selector):
"""
Menemukan element dengan CSS Selector.
"""
try:
elem = waitingResult(visibility_of_element_located, By.CSS_SELECTOR, css_selector)
return elem
except NoSuchElementException:
return None