Python 如何将FirefoxWebElememt转换为字符串类型?

Python 如何将FirefoxWebElememt转换为字符串类型?,python,selenium-webdriver,web-scraping,Python,Selenium Webdriver,Web Scraping,我在执行Python程序时遇到以下错误,该程序试图将href元素添加到基本URL以形成下一页的URL: “TypeError:无法连接'str'和'FirefoxWebElement'对象” 这是我在执行时遇到的错误: C:\Python27\python.exe C:/Python_project/8_March_2017/test_subject.py Traceback (most recent call last): 25 File "C:/Python_project/8_Marc

我在执行Python程序时遇到以下错误,该程序试图将href元素添加到基本URL以形成下一页的URL:

“TypeError:无法连接'str'和'FirefoxWebElement'对象”

这是我在执行时遇到的错误:

C:\Python27\python.exe C:/Python_project/8_March_2017/test_subject.py
Traceback (most recent call last):
25
  File "C:/Python_project/8_March_2017/test_subject.py", line 30, in <module>
    new_url = initial_url + link
TypeError: cannot concatenate 'str' and 'FirefoxWebElement' objects
C:\Python27\python.exe C:/python\u project/8\u March\u 2017/test\u subject.py
回溯(最近一次呼叫最后一次):
25
文件“C:/Python\u project/8\u March\u 2017/test\u subject.py”,第30行,在
新建url=初始url+链接
TypeError:无法连接“str”和“FirefoxWebElement”对象

您得到的错误不是selenium错误。而是简单的python字符串与非字符串对象的连接
find\u elements\u by\u xpath
提供WebElement对象的列表,而
提供get\u属性(“属性”)
提供字符串对象

由于您希望获取href而不是文本,因此可以尝试获取属性

links = driver.find_elements_by_xpath('//div/tr/td/a')
for link in links:
    new_url = base_url + link.get_attribute("href")

参考资料:

我设法更正了代码,以获得正确格式的“href”。谢谢杨。下面是我所做更改的代码

 link = browser.find_element_by_id(test1).get_attribute("href")
 print link
现在我能够获得正确的输出:

http://cbseaff.nic.in/cbse_aff/schdir_Report/AppViewdir.aspx?affno=930305
http://cbseaff.nic.in/cbse_aff/schdir_Report/AppViewdir.aspx?affno=530382

您肯定需要提供更多信息,包括您正在执行的HTML以及您正在尝试执行的具体操作。我已经更新了问题,您现在可以检查吗?在进行上述更改时,我遇到了一个新错误:
Traceback(最近一次调用):文件“C:/Python\u project/8\u March\u 2017/test\u subject.py”,第28行,在for link in links.get_attribute(“href”):AttributeError:“list”对象没有属性“get_attribute”
抱歉,我忘了提到
get_attribute
是用于WebElement对象的,而find_元素提供了WebElement的列表。所以我相应地更新了我的答案
http://cbseaff.nic.in/cbse_aff/schdir_Report/AppViewdir.aspx?affno=930305
http://cbseaff.nic.in/cbse_aff/schdir_Report/AppViewdir.aspx?affno=530382