我是Selenium / Python的新手。尝试复制数据时卡住了。我正在尝试将数据复制到列表中。每次我尝试在XPath / CSS选择器中突出显示元素名称时,它总是给我一个错误,提示“找不到元素”。有人可以帮我找到相同的确切CSS选择器/ xpath吗?以下是我为CSS选择器尝试过的代码

browser.get("http://ae.bizdirlib.com/taxonomy/term/1493") # Load page
links =[]

link = browser.find_element_by_css_selector("h2 > a")

#for link in links:
link.send_keys(Keys.CONTROL + Keys.RETURN)
link.send_keys(Keys.CONTROL + Keys.PAGE_UP)

elem = browser.find_element_by_css_selector(".content.clearfix>div>fieldset>div>ul>li>span")
elem.send_keys("bar")
elem.send_keys(Keys.CONTROL, 'a') #highlight all in box
c=elem.send_keys(Keys.CONTROL, 'c') #copy
#elem.send_keys(Keys.CONTROL, 'v') #paste
print c

最佳答案

回答第二个查询:

from selenium import webdriver

browser = webdriver.Firefox()

browser.implicitly_wait(3)

browser.get("http://ae.bizdirlib.com/taxonomy/term/1493")

links = browser.find_elements_by_css_selector("h2 > a")
links[0].click()

elems = browser.find_elements_by_css_selector("div.content.clearfix > div > fieldset> div > ul > li > span")

for elem in elems:
    print elem.text


输出:

=======

Sabbro - F.Z.C
Ajman
United Arab Emirates
Free Zone(Ajman Free Zone)
Click Here to Buy United Arab Emirates Full Data
Business Directory Database Supermarket

07-26 09:29