Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Defining function in selenium
#1
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?
Quote
#2
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
Quote
#3
(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.
Quote
#4
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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Error in Selenium: CRITICAL:root:Selenium module is not installed...Exiting program. AcszE 1 1,358 Nov-03-2017, 08:41 PM
Last Post: metulburr

Forum Jump:


Users browsing this thread: 1 Guest(s)