Python Forum
Defining function in selenium - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Defining function in selenium (/thread-28013.html)



Defining function in selenium - AgileAVS - Jul-01-2020

Hey I am very early at programming but the problem is I am having a lot of trouble while defining functions. This has made my code pages and pages long.
How do I define a function that Searches an element by xpath using selenium?
def function(self,...): 
     browser.find_element_by_xpath(“”)
     return 
How do I use it?
Element.function?


RE: Defining function in selenium - Larz60+ - Jul-01-2020

example:
from selenium import webdriver
from selenium.webdriver.common.by import By

...

browser.find_element(By.XPATH, '/html/body/div[1]/div[2]/div[1]/div/svg/g[6]/image[133]')
you can get xpath by using your browsers 'copy' in 'inspect element'
if firefox:
  • with page you wish to scrape in browser, place cursor over item you want XPath for
  • right click and select 'Inspect Element'
  • Place cursor over highlighted code
  • Right click, select copy
  • select XPath
  • Paste into code



RE: Defining function in selenium - AgileAVS - Jul-01-2020

(Jul-01-2020, 10:06 AM)Larz60+ Wrote: example:
from selenium import webdriver
from selenium.webdriver.common.by import By

...

browser.find_element(By.XPATH, '/html/body/div[1]/div[2]/div[1]/div/svg/g[6]/image[133]')
you can get xpath by using your browsers 'copy' in 'inspect element'
if firefox:
  • with page you wish to scrape in browser, place cursor over item you want XPath for
  • right click and select 'Inspect Element'
  • Place cursor over highlighted code
  • Right click, select copy
  • select XPath
  • Paste into code

I know about how to extract data using selenium and xpath, but I want to define a function that does the job for me as I input the xpath of the element. Thank you so much for your reply.


RE: Defining function in selenium - musaab - Jul-01-2020

frame.iloc(['a','b','c','d'],states)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-138-fac8bf625d88> in <module>
----> 1 frame.iloc(['a','b','c','d'],states)

TypeError: __call__() takes from 1 to 2 positional arguments but 3 were given