Основываясь на предложении @Ashish Kamble о функциональных указателях, я нашел несколько решений для моего конкретного случая.Я до сих пор не понял, как переименовать функции, чтобы наследовать исходные атрибуты class.method от существующих веб-элементов, например j.fcss("a[class='turnstileLink']").ga('title')
.
Решение исходной проблемы с другой функцией, однако я получаю:
from selenium import webdriver
d = webdriver.Firefox()
def find_css(element, css):
return element.find_element_by_css_selector(css)
def load_job_cards(counter, job_key, job_type, filestem='newjobs'):
posts = d.find_elements_by_xpath("//div[@class=' row result clickcard']")
#Breaking the Long Line with the New Function
css = "a[class='turnstileLink']"
job_names = [find_css(j, css).get_attribute('title') for j in posts]
#Other Code Where This is Also Useful
companies = [find_css(c, "span[class='company']").text for c in posts]
locations = [find_css(l, "span[class='location']").text for l in posts]
job_names = [slvm2(j, css, ga) for j in posts]
#Alt Solution 1
def find_css(element, css):
return element.find_element_by_css_selector(css)
def ga(element, attribute):
return element.get_attribute(attribute)
def load_job_cards(counter, job_key, job_type, filestem='newjobs'):
posts = d.find_elements_by_xpath("//div[@class=' row result clickcard']")
css = "a[class='turnstileLink']"
job_names = [ga(find_css(j, css), 'title') for j in posts]
#Alt Solution 2 (Less Generalizable)
def SVLM(j, css, ga):
return j.find_element_by_css_selector(css).get_attribute(ga)
def load_job_cards(counter, job_key, job_type, filestem='newjobs'):
posts = d.find_elements_by_xpath("//div[@class=' row result clickcard']")
css = "a[class='turnstileLink']"
job_names = [SVLM(j, css, 'title') for j in posts]