【问题标题】:Search for IDs from text file with python + selenium + webdriver and write to url to file使用 python + selenium + webdriver 从文本文件中搜索 ID 并将 url 写入文件
【发布时间】:2019-06-09 23:39:39
【问题描述】:

我想做什么 打开一个网址, 读取一个文本文件,一个一个地从文件中获取产品 ID, 将其输入称为网页搜索的输入字段, 点击搜索按钮, 找出页面上所有可用的供应商链接, 在新标签中打开所有供应商链接, 将这些 url 写入文件。

我尝试了什么:

import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver

from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument('--incognito')
options.add_argument("user-data-dir=d:\\abprofile")
driver = webdriver.Chrome(chrome_options=options, executable_path='C:\chromedriver_win32\chromedriver.exe')
driver.implicitly_wait(30)
driver.maximize_window()
driver.get("https://xxxxxx/products")
elem = driver.find_element_by_xpath("//input[@id='search']")
main_window = driver.current_window_handle
with open('d:/ids.txt') as in_file:
    for ids in in_file:
        elem.send_keys(ids)
        driver.switch_to.window(main_window)
        elem = driver.find_element_by_xpath("//button[contains(@id,'search')]").click()
addr = len(driver.find_elements_by_xpath("(//a[contains(.,'Available Vendors')])"))
for addrNum in range(addr):

    ele = driver.find_element_by_xpath("(//a[contains(.,'Available Vendors')])[" + str(addrNum + 1) + "]")
    href = ele.get_attribute('href')

    driver.execute_script("window.open('" + href + "');")
    time.sleep(4)
for handle in driver.window_handles:
    if handle != main_window:
        print(handle)
        page = handle
        driver.switch_to.window(page)
        time.sleep(7)
        element = driver.find_element_by_xpath("//a[contains(.,'Verfiy quotes')]").click()
        print(driver.current_url)
        urls = driver.current_url
        print (urls)
        with open('d:/somefile.txt', 'a') as the_file:
            the_file.write(urls+"\n")
driver.switch_to.window(main_window)

但是当我从文件中提交 id 时它失败了

 no such element: Unable to locate element: 
 {"method":"xpath","selector":"//button[contains(@id,'search')]"}

但是当我像下面这样只硬编码一个产品 id 时,它就可以工作了

工作:

elem.send_keys("34564545666")

不工作:

with open('d:/ids.txt') as in_file: for ids in in_file: elem.send_keys(ids) driver.switch_to.window(main_window) elem = driver.find_element_by_xpath("//button[contains(@id,'search')]").click()

任何建议,有什么问题......

【问题讨论】:

  • 您需要等待其中的元素存在吗?对于您尝试点击的元素?
  • 我相信它不是在第一次迭代中失败,而是在第二次迭代中失败。对吗?
  • 是的,第二次迭代失败了

标签: python selenium webdriver


【解决方案1】:

根本原因:这是第一个 for 循环中以下行的原因。

elem = driver.find_element_by_xpath("//button[contains(@id,'search')]").click()

在这里,您正在用按钮覆盖 elem,并尝试发送密钥。将上面的行更改为 'driver.find_element_by_xpath("//button[contains(@id,'search')]").click()`

您也可以执行以下操作。

# assign elem in the first for loop
for ids in in_file:
    editElem = driver.find_element_by_xpath("//input[@id='search']")
    editElem.send_keys(ids)

【讨论】:

  • 您好@supputuri先生,上述方法不起作用..发生的情况是它从文件中输入第一个ID,点击搜索按钮,--列出的可用供应商..--然后以错误终止定位搜索...我不确定为什么它在可用供应商页面上搜索搜索按钮。它不会出现在可用的结果中..似乎我缺少一些循环标识,不确定它在哪里.. 呵呵,但是单身完美
【解决方案2】:

当您使用 open() 读取文件时,文件末尾有 \n 字符(换行符)。

with open('d:/ids.txt') as in_file:
    for ids in in_file:
        elem.send_keys(ids)
        driver.switch_to.window(main_window)
        elem = driver.find_element_by_xpath("//button[contains(@id,'search')]").click()

print(ids) 将打印:“34564545666\n”, 不是“34564545666”

您必须使用不带 \n 字符的字符串作为参数。

所以你可以使用 ids.rstrip() (删除右端的空格。) 或 ids[:-1] (没有最后一个字符。(\n))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-06
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    • 1970-01-01
    • 2016-09-22
    • 2016-03-13
    • 1970-01-01
    相关资源
    最近更新 更多