Python/selenium: Difference between revisions

From Wiki
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 47: Line 47:
browser.find_element(By.ID, "login-signin").click()
browser.find_element(By.ID, "login-signin").click()


find_element_by_id
find_element_by_id('abc')                              <input id="abc">
find_element_by_name
find_element_by_name('abc')                            <input name="abc">
find_element_by_xpath
find_element_by_class_name('abc')                      <input class="abc">
find_element_by_link_text
find_element_by_tag_name('div')                        <div id="abc">
find_element_by_partial_link_text
find_element_by_link_text('abc')                        <a href=”a”>abc</a>
find_element_by_tag_name
find_element_by_partial_link_text('ab')                <a href=”a”>abc</a>
find_element_by_class_name
find_element_by_css_selector('p.content')              <p class="content">Site content goes here.</p>
find_element_by_css_selector
find_element_by_xpath("//form[input/@name='username']")




Line 74: Line 74:
<pre>
<pre>
.click()
.click()
.double_click()
.send_keys('hello')
.send_keys('hello')
.send_keys_to_element(element, keys)
.key_down()
.key_up()
.selected?
.selected?
.click
.click
Line 82: Line 88:
.attribute('class')
.attribute('class')
.get_attribute('innerHTML')
.get_attribute('innerHTML')
.forward()
.back()
.drag_and_drop(element, target).perform()
</pre>
</pre>
</blockquote>
</blockquote>

Latest revision as of 20:56, 29 March 2021

install

chrome

  • download chromedriver
  • unpack
  • place in '/usr/local/bin/'

PhantomJS (headless)

    • download PhantomJS and place bin/exec in /usr/local/bin
    • or install with npm:
      • apt install npm nodejs-legacy
      • npm -g install phantomjs

initialize

  • imports
import selenium
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import TimeoutException


  • initialize
def openbrowser():
    global browser
    chrome_options = Options()
    chrome_options.add_argument("--force-renderer-accessibility")
    browser = selenium.webdriver.PhantomJS()
    #browser = selenium.webdriver.Chrome('/usr/local/bin/chromedriver', chrome_options=chrome_options) # linux
    
    browser.set_window_position(1280, 0)
    browser.set_window_size(1280, 1400)


finding element

browser.find_element(By.ID, "login-signin").click()

find_element_by_id('abc')                               <input id="abc">
find_element_by_name('abc')                             <input name="abc">
find_element_by_class_name('abc')                       <input class="abc">
find_element_by_tag_name('div')                         <div id="abc">
find_element_by_link_text('abc')                        <a href=”a”>abc</a>
find_element_by_partial_link_text('ab')                 <a href=”a”>abc</a>
find_element_by_css_selector('p.content')               <p class="content">Site content goes here.</p>
find_element_by_xpath("//form[input/@name='username']") 


find_elements_by_name
find_elements_by_xpath
find_elements_by_link_text
find_elements_by_partial_link_text
find_elements_by_tag_name
find_elements_by_class_name
find_elements_by_css_selector


element operations

.click()
.double_click()

.send_keys('hello')
.send_keys_to_element(element, keys)
.key_down()
.key_up()

.selected?
.click
.clear
.displayed?
.text
.attribute('class')
.get_attribute('innerHTML')

.forward()
.back()

.drag_and_drop(element, target).perform()

other

browser.save_screenshot('filename.png')