【问题标题】:find_element_by_id().send_keys() is sending a character by character but not the whole string at a time in IEfind_element_by_id().send_keys() 在 IE 中一次发送一个字符而不是整个字符串
【发布时间】:2017-02-27 05:05:02
【问题描述】:

我想一次将字符串“abcd”发送到搜索框中,然后想单击搜索按钮,但find_element_by_id().send_keys() 正在逐个字符发送,并且需要更多时间来搜索字符串。

from openpyxl import load_workbook

from selenium import webdriver

path = raw_input()

def retrieve(path):

 wb = load_workbook(path)
    ws = wb.active

    alist, blist, clist, dlist   = []
    for row in ws.iter_rows('A{}:A{}'.format(ws.min_row,ws.max_row)):
        for cell in row:
            alist.append(cell.value)
    for row in ws.iter_rows('B{}:B{}'.format(ws.min_row,ws.max_row)):
        for cell in row:
            blist.append(cell.value)

    for row in ws.iter_rows('C{}:C{}'.format(ws.min_row,ws.max_row)):
        for cell in row:
            clist.append(cell.value)

    for row in ws.iter_rows('D{}:D{}'.format(ws.min_row,ws.max_row)):
        for cell in row:
            dlist.append(cell.value)
    driver = webdriver.Ie()
    driver.get("https://www.google.co.in/")
    for i in range(0,alist.__len__()):
        driver.find_element_by_id("lst-ib").send_keys(alist[i])
        driver.find_element_by_id("_fZl").submit()
retrieve(path)

我的 excel 工作表将有 4 列,我需要从 A1 单元格中获取字符串并在我的应用程序的搜索框中进行搜索,然后从 b1、c1、d1 单元格中填充数据,然后搜索 A2 单元格... ..

能否请您告诉我如何一次发送整个字符串。 提前致谢

【问题讨论】:

    标签: python selenium internet-explorer pycharm


    【解决方案1】:

    尝试以下方法。
    由于您的示例代码没有显示 B、C、D 值我想加入它。

    # Get Data from all rows
    for row in ws.rows:
        A_value = row[:1][0].value
        # Join columen B,C,D values without spacing
        BCD_value = ''.join(cell.value for cell in row[1:5])
    
        print('A_value=%s, BCD_value=%s' % (A_value, BCD_value))
        # Do your work with this rows Data
        # ...
        driver.find_element_by_id("lst-ib").send_keys( A_value )
    

    使用 Python:3.4.2 - openpyxl:2.4.1 测试
    如果这对您有用,请返回并将您的问题标记为已回答,或评论为什么不这样做。

    【讨论】:

    • 嗨,它仍然逐字符发送。请你再检查一次。 B,C,D 是 Excel 的列名,在从 Excel 表的 A 列中搜索字符串值后,我需要从中获取数据和处理。
    • @user62 A_value 是否显示您要发送的完整字符串?
    • 是的,stovfl,a[i] 显示的是字符串值,我要发送
    • 正如我在文档中读到的那样,.send_keys() 模拟用户键盘输入,因此设计上可能需要逐字符的行为。
    猜你喜欢
    • 2018-03-28
    • 2017-04-20
    • 1970-01-01
    • 2023-03-16
    • 2011-07-11
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多