【问题标题】:How can I put a df.iloc in this function in python如何在 python 的这个函数中放置一个 df.iloc
【发布时间】:2021-01-23 18:58:23
【问题描述】:

当我尝试将 df.iloc[0,2] 添加到 search_box.send_keys 方法时,它不起作用,并出现此错误

代码:

import time from selenium 
import webdriver ​ 

driver = webdriver.Chrome(executable_path=r'C:/webdrivers/chromedriver.exe')
driver.get('http://www.starlink.com/');
# time.sleep(5) # Let the user actually see something!
# element = element.find_elements(By.CLASS_NAME, 'foo') 
search_box = driver.find_element_by_id('service-input')
search_box.send_keys(df.iloc[0,2])
# search_box.submit()
# time.sleep(5) # Let the user actually see something!
# driver.quit()

错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-d7c71f160ef6> in <module>
      7 # element = element.find_elements(By.CLASS_NAME, 'foo')
      8 search_box = driver.find_element_by_id('service-input')
----> 9 search_box.send_keys(df.iloc[0,2])
     10 # search_box.submit()
     11 # time.sleep(5) # Let the user actually see something!

~\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py in send_keys(self, *value)
    476 
    477         self._execute(Command.SEND_KEYS_TO_ELEMENT,
--> 478                       {'text': "".join(keys_to_typing(value)),
    479                        'value': keys_to_typing(value)})
    480 

~\anaconda3\lib\site-packages\selenium\webdriver\common\utils.py in keys_to_typing(value)
    148                 typing.append(val[i])
    149         else:
--> 150             for i in range(len(val)):
    151                 typing.append(val[i])
    152     return typing

TypeError: object of type 'numpy.float64' has no len()

【问题讨论】:

  • 根据错误消息,它看起来需要string,而您正在向它发送float。尝试打印df.iloc[0,2] 的值,里面有浮点数吗?
  • 当我做 df.iloc[0,2] 我得到 35.6897
  • 再次阅读上面的评论 :-) 最终你可以尝试使用str(df.iloc[0,2]) 看看是否会变得更好
  • 你的意思是那里有一个浮子?
  • 35.6897 是所谓的float 类型。它是python中的原始数据结构之一。 datacamp.com/community/tutorials/data-structures-python

标签: python pandas dataframe selenium selenium-webdriver


【解决方案1】:

我使用了str(df.iloc[0,2]),它成功了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 2023-02-13
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2021-03-26
    相关资源
    最近更新 更多