def GetalertContext(self, driver, Writenotice): # 获取弹框 self.checkelement.Waitelement( driver, 20, By.xpath("//*[@class='alert-container']/div/div")) a = driver.findElement(By.xpath( "//*[@class='alert-container']/div")).getAttribute("class") alerttext = driver.findElement( By.xpath("//*[@class='alert-container']/div/div")).getText() if a.contains("info"): if alerttext.contains(Writenotice): return False logging.info(alerttext) return True else: logging.error(alerttext) return False
def getAlert(self, driver, Writenotice): try: # /获取弹框 self.checkelement.Waitelement( driver, 20, By.xpath("//*[@class='alert-container']/div/div")) a = driver.findElement( By.xpath("//*[@class='alert-container']/div")).getAttribute( "class") except Exception as e: return True alerttext = driver.findElement( By.xpath("//*[@class='alert-container']/div/div")).getText() if a.contains("info") | a.contains("danger"): logging.error(Writenotice + alerttext) return False else: logging.info(Writenotice + alerttext) return True
def login(driver, email=None, password=None): if not email or not password: email, password = __prompt_email_password() driver.get("https://www.linkedin.com/login") element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "username"))) email_elem = driver.find_element_by_id("username") email_elem.send_keys(email) password_elem = driver.find_element_by_id("password") password_elem.send_keys(password) password_elem.submit() time.sleep(5) print(driver.current_url) try: WebDriverWait(driver, 0).until( EC.presence_of_element_located( (By.CLASS_NAME, "remember-me-prompt"))) driver.findElement(By.xpath("//*[contains(text(),'Not Now')]")).click() except: try: WebDriverWait(driver, 0).until( EC.presence_of_element_located( (By.CLASS_NAME, "cp-challenge"))) driver.findElement( By.xpath("//*[contains(text(),'Skip')]")).click() except: element = WebDriverWait(driver, 0).until( EC.presence_of_element_located( (By.ID, "global-nav-typeahead"))) finally: element = WebDriverWait(driver, 0).until( EC.presence_of_element_located((By.ID, "global-nav-typeahead"))) element = WebDriverWait(driver, 0).until( EC.presence_of_element_located((By.ID, "global-nav-typeahead")))
def get_search_result(self): # 暂时发现存在两种布局的商品搜索结果,由try进行区分 try: product_name_div = self.wait.until( EC.presence_of_element_located((By.XPATH, "//*[@id='search']/div[1]/div[2]/div/span[3]/div[1]/div/div/div/div/div[2]/div[2]/div/div[1]/h2/a/span")) ) except Exception as e: product_name_div = self.wait.until( EC.presence_of_element_located((By.XPATH, "//*[@id='search']/div[1]/div[2]/div/span[3]/div[1]/div/div/div/div/div[2]/div[2]/div/div[1]/div/div/div[1]/h2/a/span")) ) finally: # 点击进入所搜索商品的页面 product_name_div.click() product_name__detail_div = self.browser.findElement(By.xpath("//*[@id=‘acrCustomerReviewText’]")) product_name__detail_div.click()
def gmail_login(): dr.get("https://accounts.google.com/signin/v2/identifier?hl=en&passive=true&continue=https%3A%2F%2Fwww.google.com%2F%3Fgws_rd%3Dssl&ec=GAZAAQ&flowName=GlifWebSignIn&flowEntry=ServiceLogin") ele=dr.find_element_by_id('identifierId') ele.send_keys('') #your mail goes here btn=dr.find_element_by_id('identifierNext').click() password=dr.find_element_by_xpath('//*[@id="password"]/div[1]/div/div[1]/input') password.click() password.send_keys('') #your passwrod goes Here! nextButton = dr.find_elements_by_xpath('//*[@id ="passwordNext"]') nextButton[0].click() WebElement element = dr.findElement(By.xpath('//*[@id ="passwordNext"]')); Actions actions = new Actions(dr); actions.moveToElement(element).click().build().perform();
= 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0' ghost = webdriver.PhantomJS(executable_path='E:/software/phantomjs-2.1.1-windows/bin/phantomjs.exe') def get_param( driver, url ): driver.get(url) total_txt = driver.find_element_by_id("total").text #try: return total_txt if __name__ == '__main__': cid = 566921 url_address = "https://iknow.jp/courses/%d"%cid #total = get_param(ghost,url_address) # f = open("record.txt","wt+",encoding="UTF-8") #ofile_name = "record_%d.txt"%cid #ofile = codecs.open(ofile_name, "w", "utf-8") voclist = ghost.findElements(By.xpath("//li")) for i in voclist: print(i) #for j in range(0, total): current_page = ghost.find_element_by_css_selector('.items').text print("current page:" + current_page) k = 0 # get_transaction_from_page(ofile, ghost, int(current_page), total) ghost.close() #ofile.close()
def get_parent(self): return self.web_element.find_element(By.xpath('..'))
def get_items(self) -> list: li_list = self.web_element.find_elements(By.xpath('/li')) return DomElementFactory.create_dom_element_list(li_list)
password = driver.find_element_by_id("password") #fills out the username and password forms and "clicks" the link username.send_keys('_________') password.send_keys('__________') driver.find_element_by_name("signIn").click() #makes sure the DOM has been fully loaded before interacting with the site time.sleep(3) driver.get(firstLink) time.sleep(3) driver.get(secondLink) time.sleep(3) driver.get(thirdLink) #checks for how many pages to screenshot numberIter = int(input("how many _______?")) for num in range(1, numberIter + 1): #dynamically resizes the page to screenshot it in its entirety total_width = driver.execute_script( "return document.body.offsetWidth") total_height = driver.execute_script( "return document.documentElement.scrollHeight") driver.set_window_size(total_width, total_height) #screenshots the page fileName = r"{}\a{}.png".format(aFolder, str(num)) driver.save_screenshot(fileName) #finds the next button driver.findElement(By.xpath('//a[@class = "Next"]')).click()
# 5:E:last-child,最后一个标签 # 6:E:only-child,唯一的标签 ''' 元素操作的几个方法''' driver.find_element_by_id("kw").clear() driver.find_element_by_id("kw").send_keys("1") driver.find_element_by_id("su").click() driver.find_element_by_id("su").submit() # submit()要求提交对象是一个form表单,更强调对信息的提交。click()更强调事件的独立性。(比如,一个文字链接就不能用 submit()方法。) ''' https://www.cnblogs.com/constantince/p/4565261.html chrome浏览器开发这使用指南 ''' ''' 第一种方法:通过绝对路径做定位(相信大家不会使用这种方式) By.xpath(r"/html/body/div/form/input") 第二种方法:相对路径 By.xpath("//input") 第三种方法:通过元素索引定位 By.xpath("//input[4]") 第四种方法:使用xpath属性定位(结合第2、第3中方法可以使用) By.xpath("//input[@id='kw1']") By.xpath("//input[@type='name' and @name='kw1']") 第五种方法:使用部分属性值匹配(最强大的方法) By.xpath("//input[starts-with(@id,'nice')") # By.xpath("//input[ends-with(@id,'很漂亮')") 貌似没有 By.xpath("//input[contains(@id,'那么美')]") 前一个兄弟节点 //input[@name="sne.sysLnNumAdmin.category2"]/preceding-sibling::input
from bs4 import BeautifulSoup import requests from urllib.request import urlopen import re html_page = urlopen("https://astrogeology.usgs.gov/search/results?q=hemisphere+enhanced&k1=target&v1=Mars") soup = BeautifulSoup(html_page, 'lxml') links = [] for link in soup.findAll('a', attrs={'href': re.compile("^http://")}): links.append(link.get('href')) print(links) #%% # browser.click_link_by_partial_href("/search/map/Mars/Viking/cerberus_enhanced") elem = browser.find_element_by_xpath("//*[@id="product-section"]/div[2]/div[1]/a") # JavascriptExecutor executor = (JavascriptExecutor)driver; # executor.executeScript("arguments[0].click();", ele); # //*[@id="product-section"]/div[2]/div[1]/a # #product-section > div.collapsible.results > div:nth-child(1) > a #%% WebElement parent = driver.findElement(By.xpath("//a/li")); WebElement date = parent.findElement(By.xpath("div/span[contains(@class, 'child-date')]")); WebElement date = parent.findElement(By.xpath("//span[contains(@class, 'child-date')]"));
driver.find_element_by_id("com.jiandan.mobilelesson:id/province").click() driver.find_element_by_id("com.jiandan.mobilelesson:id/city").click() driver.find_element_by_id("com.jiandan.mobilelesson:id/district").click() driver.find_element_by_id("com.jiandan.mobilelesson:id/confirm").click() iphone = "13344" + str(random.randint(100000, 999999)) driver.find_element_by_id("com.jiandan.mobilelesson:id/cellphone").send_keys( iphone) driver.find_element_by_id("com.jiandan.mobilelesson:id/getcheckcode").click() # driver.get_screenshot_as_file("./image/valid.png") mobile_message = r"验证码已发送到您的手机" message1 = '//*[@text=\'{}\']'.format(mobile_message) try: toast_element1 = WebDriverWait(driver, 8, 0.01).until( EC.presence_of_element_located(By.xpath(message1))) except: driver.get_screenshot_as_file("./image/valid_error.png") else: print(toast_element1.text) driver.find_element_by_id("com.jiandan.mobilelesson:id/checkcode").send_keys( "123456") driver.find_element_by_id("com.jiandan.mobilelesson:id/use_regist").click() # driver.get_screenshot_as_file("./image/toast.png") error_message = r"短信验证码错误" message2 = '//*[@text=\'{}\']'.format(error_message) try: toast_element2 = WebDriverWait( driver, 8, 0.01).until(lambda x: x.find_element_by_xpath(message2)) except:
url = (rawTitle.get_attribute("href")) ### Going to article page driver1.get(url) time.sleep(3) actualArticlePages = driver1.find_elements_by_xpath("//div[@class = 'icons portlet']/a") if(len(actualArticlePages) != 0): ### Find the link for PMC, not other journals actualArticlePage = actualArticlePages[-1] ### Find the title of the article title = driver.findElement(By.xpath("//div[contains(text(),'"+"title"+"')]")).text; ### Find the journal of the article, in case needed for further usage #journal = actualArticlePage.get_attribute("journal") #print(journal) ### Get the article name name = title+"pdf" k = k + 1 ### "Further usage: get articles from journals other than PMC #if journal =="Cells" or journal == "Vaccine" or journal == "BMJ Open" or journal == "Biotechnol Biofuels" or journal == "Asian-Australas J Anim Sci": ### Go to actual download page
driver = webdriver.Chrome(chrome_options=chrome_options) driver.maximize_window() driver.get("https://fyonecardpre.cdwit120.com?redirect=mine") driver.find_element_by_class_name('link').click() driver.find_element_by_xpath('//*[@id="app"]/div/footer/a[1]/div[2]').click() time.sleep(2) driver.find_element_by_xpath('//*[@id="app"]/div/footer/a[2]/div[1]/div').click() time.sleep(2) driver.find_element_by_xpath('//*[@id="app"]/div/footer/a[3]/div[1]/div').click() time.sleep(2) driver.find_element_by_xpath('//*[@id="app"]/div/footer/a[4]/div[1]/div').click() time.sleep(2) driver.find_element(By.CLASS_NAME("input.fy-flex-2")).clear(); driver.findElement(By.cssSelector("input.fy-flex-2")).sendKeys("13333333338"); driver.findElement(By.xpath("//input[@type='password']")).clear(); driver.findElement(By.xpath("//input[@type='password']")).sendKeys("111111"); driver.findElement(By.xpath("//div[@id='app']/div/div[2]/button")).click(); driver.findElement(By.cssSelector("div.header-content")).click(); driver.findElement(By.linkText("退出登录")).click(); driver.findElement(By.cssSelector("input.fy-flex-2")).clear(); driver.findElement(By.cssSelector("input.fy-flex-2")).sendKeys("13333333338"); driver.findElement(By.xpath("//input[@type='password']")).clear(); driver.findElement(By.xpath("//input[@type='password']")).sendKeys("111111"); driver.findElement(By.xpath("//div[@id='app']/div/div[2]/button")).click(); driver.findElement(By.cssSelector("div.link")).click(); driver.findElement(By.cssSelector("div.mine-icon.icon-img")).click(); driver.findElement(By.cssSelector("img.img")).click(); driver.findElement(By.linkText("退出登录")).click(); driver.findElement(By.cssSelector("input.fy-flex-2")).clear(); driver.findElement(By.cssSelector("input.fy-flex-2")).sendKeys("13333333338");