03
2021
12

Python selenium 瀏覽器自動化

update for colab chrome


!apt-get update

!apt install chromium-chromedriver

!cp /usr/lib/chromium-browser/chromedriver /usr/bin

!pip install selenium


from selenium import webdriver

from selenium.webdriver.common.by import By

options = webdriver.ChromeOptions()

options.add_argument('--headless')

options.add_argument('--no-sandbox')

options.add_argument('--disable-dev-shm-usage')

driver = webdriver.Chrome('chromedriver',options=options)

import re

import time




https://www.796t.com/article.php?id=101939


https://justcode.ikeepstudying.com/2019/09/python-selenium%E5%A4%9A%E7%AA%97%E5%8F%A3%E4%B9%8B%E5%89%8D%E7%9A%84%E5%88%87%E6%8D%A2-webdriver%E7%AA%97%E5%8F%A3%E5%88%87%E6%8D%A2/


https://justcode.ikeepstudying.com/2019/09/python%EF%BC%9A%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8selenium%E5%9C%A8iframe%E4%B9%8B%E9%97%B4%E5%88%87%E6%8D%A2-selenium%E8%8E%B7%E5%8F%96iframe-webdriver-how-to-switch-between-iframes-using-seleniu/


https://www.cnblogs.com/yoyoketang/p/14124336.html



https://www.i4k.xyz/article/weixin_49167820/117437965




https://blog.csdn.net/HHG20171226/article/details/102623940


input = driver.find_element(By.XPATH,'query_ticket')  or driver.find_element(By.TAG_NAME,'query_ticket')  or driver.find_element(By.NAME,'query_ticket') or driver.find_element(By.CLASS_NAME,'query_ticket')  or driver.find_element(By.CSS_SELECTOR,'query_ticket')

input.send_keys("13728556386")

btn = driver.find_element(By.ID,'query_ticket')   ==  driver.find_element_by_id('query_ticket')

driver.execute_script('arguments[0].click()',btn)

#or driver.execute_script('xxx_copy from js path_xxx.click()',btn)



button = driver.wait.until(EC.presence_of_element_located(

    (By.XPATH, "//div[@id='pagination-element']/nav[1]/span[3]/button[1]/span[1]/i[1]")

))

driver.execute_script("arguments[0].click();", button)


driver.execute_script("window.scrollTo(0, Y)") Y is the vertical position where you wanna move to


WebDriverWait(driver,10).until(expected_conditions.presence_o f_element_located((By.ID,'xxxx')))


driver.execute_script('''document.querySelector("button[type='submit']").click();''')


wd_elements(By.CSS_SELECTOR, "#data-util-col > section:nth-child(6) > table > tbody > tr:nth-child(2) > td.Va\(t\).Fz\(14px\).Whs\(nw\).Py\(6px\).Ta\(start\).Start\(0\).Pend\(10px\) > a").click()


#wd.find_element(By.ID, "id")

#wd.find_element(By.NAME, "name")
#wd.find_element(By.XPATH, "xpath")
#wd.find_element(By.LINK_TEXT, "link text")
#wd.find_element(By.PARTIAL_LINK_TEXT, "partial link text")
#wd.find_element(By.TAG_NAME, "tag name")
#wd.find_element(By.CLASS_NAME, "class name")
#wd.find_element(By.CSS_SELECTOR, "css selector")


e.g. 

elem = driver.find_element_by_css_selector("button[type='submit']")

driver.execute_script("arguments[0].click()", elem)


WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.CSS_SELECTOR , '.flt-subhead1.gws-flights-results__price.gws-flights-results__cheapest-price span + jsl')))





Chrome browser example:


<link rel="multiply" type="service/math" src="path/to/service">

<link rel="substract" type="service/math" src="path/to/service">

...



trying to use querySelectorAll to retrieve all link elements with a type service/... specified


e.g. ... document.querySelectorAll( 'link' )  #return a list

document.querySelector( 'link' )  #return an element

document.querySelector( 'link' ).click()  #click an element button

document.querySelector( 'link' ).value='xxxxx'  #input sth. into an input box

document.querySelectorAll( 'link' ).innerText  #get the pure text of HTML

document.querySelectorAll( 'link' ).innerHTML #get the corresponding HTML code with text


document.getElementById

document.getElementsByName

document.getElementsByTagName

document.getElementsByClassName



.  <-for class selector

#  <-for id selector

[... document.querySelectorAll( 'link' )] #three dots before aims to return a normal list


Filtering the ones with type service/.* ?


You can use a substring matching attribute selectors : link[type^=service]

"Nodes of type link with an attribute type starting with the letter "service"   -just like a regular expression

g. document.querySelectorAll( '[ng-controller^="' + <controller name> + '"' )



python selenium

driver.execute_script("""return document.getElementById('kw').value""")





switch to the iframe window

driver.switch_to.frame(driver.find_element_by_css_selector(css_selector))


switch back to the main window

driver.switch_to.default_content()


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。