def fetch_flight_data(webdriver): table_elem = webdriver.find_element(By.ID, 'faresOutbound') soup = BeautifulSoup(table_elem.get_attribute('innerHTML')) result_rows = soup.findAll('tr', id=lambda x: x and 'outbound_flightRow' in x) for elem in result_rows: # get departure time depart_time = elem.find('td', { 'class': 'depart_column' }).find('span', {'class', 'bugText'}).get_text() print 'departure time: ' + depart_time # get arrival time arrive_time = soup.find('td', { 'class': 'arrive_column' }).find('span', {'class', 'bugText'}).get_text() arrive_time = arrive_time.replace('\n', ' ').strip() # clean up print 'arrival time: ' + arrive_time # get flight number(s) flight_info = soup.find('td', {'class': 'flight_column'}) # get all flights (connecting/final) flights = flight_info.find_all( 'div', class_=lambda x: x and 'aircraft_information' and 'right' in x) flight_info_dict = {} index = 0 for flight_info in flights: flight_num, plane_type, seat_num = parse_flight_info( flight_info.text) flight_info_dict[index] = [flight_num, plane_type, seat_num] for key in flight_info_dict.keys(): print 'flight number: ' + flight_info_dict[key][0] print 'plane type: ' + flight_info_dict[key][1] print 'number seats: ' + flight_info_dict[key][2] # get number of connecting stops connect_num = soup.find('div', { 'class': 'flightDetailsContainer' }).find('span', {'class': 'headerText'}) connect_num = connect_num.text.strip('(').split()[0] # get connecting stops connecting_stops = parse_routing_info( soup.find('table', {'class': 'routingDetailsContainer'}), int(connect_num))
def replied(): # _3ExzF # # document.getElementsByClassName("_3XpKm _20zqk")[11].children[0].getAttribute("aria-label") input("Press") sleep(5) ele = webdriver.find_element( By.XPATH, "/html/body/div/div[1]/div[1]/div[4]/div[1]/div[3]/div/div/div[3]/div[20]/div/div/div/div[1]/div" ) print(ele) print(ele.text) ele
def get_table_content(tableId, queryContent): arr = [] arr1 = [] table_loc = webdriver.find_element_by_id(tableId) # 按行查询表格的数据,取出的数据是一整行,按空格分隔每一列的数据 table_tr_list = webdriver.find_element(*table_loc).find_elements( By.TAG_NAME, "tr") for tr in table_tr_list: arr1 = (tr.text).split(" ") #以空格拆分成若干个(个数与列的个数相同)一维列表 print(tr.text) # print(arr1) arr.append(arr1) #将表格数据组成二维的列表 #循环遍历table数据,确定查询数据的位置 for i in range(len(arr)): for j in range(len(arr[i])): if queryContent == arr[i][j]: print("%r坐标为(%r,%r)" % (queryContent, i + 1, j + 1))
def checkOnline(): while (True): # _7yrSq _3-8er selectable-text copyable-text try: # 86400 # isOnline = WebDriverWait(webdriver, 86400).until(EC.presence_of_element_located(( # By.XPATH, '/html/body/div/div[1]/div[1]/div[4]/div[1]/header/div[2]/div[2]/span'))) isOnline = webdriver.find_element( By.XPATH, '/html/body/div/div[1]/div[1]/div[4]/div[1]/header/div[2]/div[2]/span' ) if isOnline != None: return True except Exception as e: # print("Error checking online ") # print(e) return False
webdriver.get('https://www.skincare-junkie.com/new-members') sleep(5) for i in range(1, 2): webdriver.execute_script( "window.scrollTo(0, document.body.scrollHeight);") sleep(randint(1, 5)) elements = webdriver.find_elements(By.CLASS_NAME, 'member-list-item') user_ids = [x.get_attribute('data-member-item') for x in elements] sleep(randint(10, 30)) webdriver.get('https://www.skincare-junkie.com/sign_in') sleep(5) email_field = webdriver.find_element(By.CLASS_NAME, 'email-input') email_field.send_keys('*****@*****.**') next_button = webdriver.find_element(By.CLASS_NAME, 'submit-button') next_button.click() password_field = webdriver.find_element(By.CLASS_NAME, 'password-input') password_field.send_keys('5?58BHDq@Z4FkKW') sleep(randint(3, 5)) next_button = webdriver.find_element(By.CLASS_NAME, 'submit-button') next_button.click() sleep(randint(10, 20)) count = 0 for user_id in user_ids: webdriver.get('https://www.skincare-junkie.com/chats/new?user_id=' + user_id)
# chrome_options.add_argument("--headless") chrome_options.add_argument("--window-size=1920x1080") chrome_options.add_argument( "--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36" ) driver = webdriver.Chrome(options=chrome_options, executable_path='./chromedriver') # setting up test meeting driver.get("https://www.webex.com/test-meeting.html") driver.find_element(By.ID, "full-name").send_keys("Gerard Kensington") driver.find_element(By.ID, "email-address").send_keys("*****@*****.**") driver.find_element(By.CSS_SELECTOR, ".form_button").click() time.sleep(4) driver.find_element(By.ID, "push_download_webapp_buttom_join_link").click() driver.find_element(By.CSS_SELECTOR, ".style-arrow-2f8xt > .style-icon-2HDxh").click() element = webdriver.find_element(By.CSS_SELECTOR, ".style-arrow-2f8xt > .style-icon-2HDxh") actions = webdriver.ActionChains(driver) actions.move_to_element(element).perform() element = driver.find_element(By.CSS_SELECTOR, "body") actions = webdriver.ActionChains(driver) actions.move_to_element(element, 0, 0).perform() driver.find_element(By.CSS_SELECTOR, "li:nth-child(3)").click() driver.find_element(By.ID, "interstitial_join_btn").click() """ <a id="push_download_webapp_buttom_join_link" href="javascript:void(0);">Join from your browser</a> """
from selenium import webdriver from selenium.webdriver.chrome.webdriver import WebDriver from selenium.webdriver.common.by import By webdriver = webdriver.Chrome() # type: WebDriver #Assert.assertEquals(expectedUrl, driver.getCurrentUrl()); #website = websiteDesired #print ("enter website: ") #input (website) #print (website) webdriver.get("http://www.google.com") webdriver.find_element(By.NAME, "q").click() webdriver.find_element(By.NAME, "q").send_keys("wikipedia") webdriver.find_element(By.NAME, "btnK").click() #trying to find a clickable first link, by copying JS and XPath from CSS #document.querySelector('#rso > div:nth-child(1) > h2') #//*[@id="cnt"]/div[9] Xpath #document.querySelector('#rso > div:nth-child(1) > div > link') #webdriver.find_element(By.CSS_SELECTOR, "#rso > div:nth-child(1) > h2").click() #webdriver.find_element(By.CSS_SELECTOR, "#rso > div:nth-child(1) > div > link").click() #document.querySelector('#rso > div:nth-child(1) > div > div > div.r > a') #found a clickable element! webdriver.find_element( By.CSS_SELECTOR, "#rso > div:nth-child(1) > div > div > div.r > a").click()
def find_element(): element = webdriver.find_element(By.ID, "xx") style_yellow = 'arguments[0].style.border="5px solid yellow"' webdriver.execute_script(style_yellow, element) print("我在find element")
webdriver.find_element_by_tag_name("") webdriver.find_element_by_link_text("") webdriver.find_element_by_partial_link_text("") webdriver.find_element_by_css_selector("") webdriver.find_element_by_xpath("") # 定位一组元素,返回对象列表 8种 webdriver.find_elements_by_id("") # id复数定位 webdriver.find_elements_by_name("") # name复数定位 webdriver.find_elements_by_class_name("") # class复数定位 webdriver.find_elements_by_tag_name("") # teg复数定位 webdriver.find_elements_by_link_text("") # link复数定位 webdriver.find_elements_by_partial_link_text("") # partial_link复数定位 webdriver.find_elements_by_css_selector("") # css_selector 复数定位 webdriver.find_elements_by_xpath("") # xpath复数定位 # 这两种是参数化的方法,对上面各8种的总结 webdriver.find_element(by='id', value="") webdriver.find_elements(by='id', value="") ''' 1、xpath语法: 指明标签 //*或者 //input[@id="kw"] 根据@属性定位,id name class 或其他属性 逻辑运算and or not,用的最多的是and,同时满足两个属性,//*[@id="kw" and @name="aa"] 层级定位/, 索引定位,从1开始,input[1],多个相同标签,用索引定位 模糊匹配:标签对之间的文本信息的模糊匹配//*[contains(text(),"hao123)] 模糊匹配某个属性//*[contains(@id,"kw")] 模糊匹配以什么开头//*[starts-with(@id,"kw")] 模糊匹配以什么结尾//*[ends-with(@id,"kw")] 还支持最强的正则表达式 //*[match(text(),"kw")] 后边是我的猜测//*[match(@id,"^kw")] 2、css选择器语法:官方说法,css定位更快,语法更简洁,但是xpath更直观,更好理解一些。
response = urllib2.urlopen(request) print(response.read()) import requests from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By url1 = "https://www.rakuten.com.tw/shop/food-collect/product/apple2017072100/?s-id=Event-supersale-170814-index-081820-003" ChromeDriver = "D:/chromedriver_win32/chromedriver.exe" browser = webdriver.Chrome(executable_path=ChromeDriver) browser.get(url1) #assert "afv" in browser.title webdriver.find_element(By.XPATH, '//button[text()="Some text"]') browser.find_elements(By.XPATH, '//button') browser.find_element_by_class_name("b-btn") elem = browser.find_element_by_name("q") elem.send_keys("pycon") elem.send_keys(Keys.RETURN) print(browser.page_source) class PythonOrgSearch(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() def test_search_in_python_org(self): driver = self.driver
5).until(ec.presence_of_element_located( (By.ID, "check"))) driver.find_element(By.ID, "check").click() WebDriverWait(driver, 5).until(ec.element_to_be_clickable((By.ID, "nextword"))) driver.find_element(By.ID, "nextword").click() else: return learn(driver, questions, answers) return {"questions": questions, "answers": answers} if __name__ == "__main__": settings = argument_parse() webdriver = webdriver_generate(settings["headless"]) lists = read_words(settings["file"]) login(webdriver, settings["username"], settings["password"]) while True: try: lists = answer(webdriver, lists["questions"], lists["answers"], settings["delay"]) except (ElementNotInteractableException, TimeoutException): dismiss_popup(webdriver) if webdriver.find_element(By.ID, "return_mainpage").is_displayed(): settings["sessions_to_do"] = session_end( webdriver, settings["sessions_to_do"], settings["file"], lists["questions"], lists["answers"], )
def southwest_search(webdriver, one_way, origin, destination, departure_date, return_date): # first have to make sure we can send keys to boxes WebDriverWait(webdriver, 10).until( EC.element_to_be_clickable((By.NAME, 'originAirport'))) # specify if looking for 1 way or round trip if one_way: webdriver.find_element(By.ID, "trip-type-one-way").click() else: webdriver.find_element(By.ID, "trip-type-round-trip").click() # make sure we've deleted all pre-inputted input N = 9 webdriver.find_element(By.NAME, 'originAirport').send_keys(Keys.BACKSPACE * N) webdriver.find_element(By.NAME, 'destinationAirport').send_keys(Keys.BACKSPACE * N) webdriver.find_element(By.ID, 'air-date-departure').send_keys(Keys.BACKSPACE * N) if not one_way: webdriver.find_element(By.ID, 'air-date-return').send_keys(Keys.BACKSPACE * N) # input specified parameters webdriver.find_element(By.NAME, 'originAirport').send_keys(origin) webdriver.find_element(By.NAME, 'destinationAirport').send_keys(destination) webdriver.find_element(By.ID, 'air-date-departure').send_keys( departure_date) #has to be MM/DD format! if not one_way: webdriver.find_element(By.ID, 'air-date-return').send_keys(return_date) webdriver.find_element(By.ID, 'jb-booking-form-submit-button').send_keys( Keys.ENTER)