Python Forum

Full Version: Defining function in selenium
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
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
(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.
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