Showing posts with label Seliunm. Show all posts
Showing posts with label Seliunm. Show all posts

Saturday, May 15, 2021

Selenium Xpath



 Today I wants to share Xpath using selenium. Xpath is a unique tool, which is better than other finding_element. These are the following element you can use:

find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector

or multiply element 

find_elements_by_id
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


I think to know XPath is really important.

The syntax of it is: 

Xpath=//tagname[@attribute='value']

This is a HTML :

<html>
 <body>
  <p>Are you sure you want to do this?</p>
  <a href="continue.html">Continue</a>
  <a href="cancel.html">Cancel</a>
  <form id="loginForm">
    <input name="username" type="text" />
    <input name="password" type="password" />
    <input name="continue" type="submit" value="Login" />
    <input name="continue" type="button" value="Clear" />
    <div class="search">
      This is new page<h1>Hello</h1>!!!!!
    </div>
</form>
</body>
</html>

  • If you want to get the name for the username, you can try like this:

//input[@name='username']

  • using chrome development tool to get xpath. The [1] is username, [2] is password ....[4]

//*[@id="loginForm"]/input[1]
//*[@id="loginForm"]//input[@name="username"]

  • Full path using chrome development tool

/html/body/form/input[1]

you can see the input name for clear and login is the same "continue", if we want to get the clear button:

//input[@name='continue'][@type='button']

or

//*[@id="loginForm"]/input[4]


You can also use some function text(), or contain

If you use JUST text(), the string must be the same, else won't find it

 //*[text()="str"]
 //*[contains(text(),'str')]

if I want to find "continue", I can use this:

//*[text()="Continue"]

or 

 //*[text()="Cont"] 

using (dot)

Sometimes when you used  text() or contain() to find, but will not be able to find, you can use this method:

For example, this is Instagram following text, which can't find the following, you have to use this method. 


//*[text()[contains(.,'str')]]

//*[contains(.,'str')]


this will search from the root to the current node. 

Selenium with Xpath

inorder to use in selenium you have to use like this|:

driver.find_element_by_xpath('put-your-xpath-here').click()



Thursday, June 4, 2020

python changed pip

if our install pyton2 and python3 on your PC, it might used python2's pip. you can used the command

pip --version
D:\selenium-3.141.0.tar\dist\selenium-3.141.0>pip --version
pip 20.1.1 from c:\python27\lib\site-packages\pip (python 2.7)

you can also do like this :
#python36\Scripts\pip.exe install packagename
Example:
C:\python37\Scripts>pip3.exe install packagename

reference:
https://stackoverflow.com/questions/39851566/using-pip-on-windows-installed-with-both-python-2-7-and-3-5
https://stackoverflow.com/questions/40832533/pip-or-pip3-to-install-packages-for-python-3

selenium problem

This is a interesting topic and funny thing about selenium, after surfing on the net, i find this article which really solve the problem.


Problem: Used pip to install selenium and show install success. But module ONLY work on Python2 BUT Python3 DON'T work. Sound really strange, isn't.  
Solution: So just download selenium package and manual install. 
How: Just extract the file and go to the directory and used the command will install:
python
Conclusion is we have to manual install selenium . 



Wednesday, June 3, 2020

Seliunm

Chrome diver: chromedriver
https://chromedriver.chromium.org/downloads

Firefox driver: geckodriver
https://github.com/mozilla/geckodriver/releases

Basic Selenium
from selenium import webdriver
browser=webdriver.Chrome('D:\\chromedriver.exe')
browser.get('http://google.com')
browser.quit() 

Selenium with beautifulsoup example 1: will pop chrome 


from selenium import webdriver
from bs4 import BeautifulSoup
try:
    chrome=webdriver.Chrome(executable_path='D:\\CHROME_DRIVER\\chromedriver.exe')
    chrome.set_page_load_timeout(10)
    chrome.get('https://code-gym.github.io/spider_demo/')
    soup = BeautifulSoup(chrome.page_source, 'html5lib')
    print(soup.find('h1').text)
finally:
browser.quit() 


Selenium with beautifulsoup example 2: will run chrome at daemon
from selenium import webdriver
from bs4 import BeautifulSoup
try:
    options = webdriver.ChromeOptions()
    options.add_argument('--headless')     
    chrome=webdriver.Chrome(options=options,executable_path='D:\\CHROME_DRIVER\\chromedriver.exe')
    chrome.set_page_load_timeout(10)
    chrome.get('https://code-gym.github.io/spider_demo/')
    soup = BeautifulSoup(chrome.page_source, 'html5lib')
    print(soup.find('h1').text)
finally:
browser.quit() 

Selenium with beautifulsoup using xpath to find related article 


from selenium import webdriver
from bs4 import BeautifulSoup
try:
.    options = webdriver.ChromeOptions()
    ..........................
    ..........................
    ..........................
  print(soup.find('h1').text)
  chrome.find_element_by_xpath('/html/body/div[2]/div/div[1]/div[1]/div/div/h3/a').click(
  print(chrome.find_element_by_xpath('//*[@id="post-header"]/div[2]/div/div/h1').text)
finally:
browser.quit()