grey_lemon 发表于 2021-5-31 09:14:50

Python web自动化 完善

哪位大神帮忙看下怎么完善以下代码
共向网页发送两次字符串, 但是打印结果只是第二次发送的字符串相应的网页元素。
请问怎么做到打印出第一次和第二次发送的字符串相应的网页元素?谢谢!

from selenium import webdriver

wd = webdriver.Chrome(r'C:\Users\sesa395799\Downloads\chromedriver_win32\chromedriver.exe')

wd.get('https://www.digikey.cn/zh/products/category/resistors/2')
mainWindow = wd.current_window_handle

element = wd.find_element_by_css_selector("#header > div.header__top > div.header__search > div > div.searchbox-inner > div.searchbox-inner-searchtext > input")
element.send_keys('12FR010E-ND\n')


wd.switch_to.window(mainWindow)
element = wd.find_element_by_css_selector("#header > div.header__top > div.header__search > div > div.searchbox-inner > div.searchbox-inner-searchtext > input")
element.send_keys('CF1/4CT52R101J\n')

elements =wd.find_elements_by_css_selector('#product-attributes > tbody > tr > td > div')

for element in elements:
    print(element.text)
wd.quit()

grey_lemon 发表于 2021-5-31 09:16:39

或者怎么把两个字符串(12FR010E-ND,CF1/4CT52R101J )以列表的形式, 分别发送至网页查询?

suchocolate 发表于 2021-5-31 10:33:57

本帖最后由 suchocolate 于 2021-5-31 10:58 编辑

代码只是参考,我们没有chrome,不能帮你验证。
from selenium import webdriver


def main():
    wd = webdriver.Chrome(r'C:\Users\sesa395799\Downloads\chromedriver_win32\chromedriver.exe')
    wd.get('https://www.digikey.cn/zh/products/category/resistors/2')
    result = []   # 新建一个结果列表
    for s in ['12FR010E-ND\n', 'CF1/4CT52R101J\n']:   # 对字符串列表循环,逐个访问
      wd.switch_to.window(wd.current_window_handle)
      element = wd.find_element_by_css_selector(
            "#header > div.header__top > div.header__search > div > div.searchbox-inner > div.searchbox-inner-searchtext > input")
      element.send_keys(s)   # 发送当前字符s到输入框
      # 我理解这里应该有click模拟点击。
      # 另外还有要等待ec函数等待结果。
      result.append(wd.find_elements_by_css_selector('#product-attributes > tbody > tr > td > div'))# 把找到的元素放到result
   
    for e in result:   #总结果列表的元素遍历,逐个处理。
      print(e.text)
    wd.quit()


if __name__ == "__main__":
    main()

grey_lemon 发表于 2021-5-31 11:22:07

报错 AttributeError: 'list' object has no attribute 'text' 'list' object has no attribute 'text'

还有个问题, 如果列表有4个元素, ['12FR010E-N\n','MP930-15.0F-ND\n','MR40MB12.70R001JT\n','RNF18FTD1K00\n'], 其中第三个在网页上是找不到的, 怎么添加判断, 路过然后继续查找第四个元素?

谢谢

suchocolate 发表于 2021-5-31 11:26:57

grey_lemon 发表于 2021-5-31 11:22
报错 AttributeError: 'list' object has no attribute 'text' 'list' object has no attribute 'text'

...

from selenium import webdriver
from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support import expected_conditions as expected
from selenium.webdriver.support.wait import WebDriverWait


def main():
    browser = webdriver.Firefox()
    ip = "#header > div.header__top > div.header__search > div > div.searchbox-inner > div.searchbox-inner-searchtext > input"   # 查找input的css选择器字符串
    rs = "#product-attributes > tbody > tr > td > div"   # 查找结果的css选择器字符串
    browser.get('https://www.digikey.cn/zh/products/category/resistors/2')
    wait = WebDriverWait(browser, 30)   # 创建等待对象
    wait.until(expected.visibility_of_element_located((By.CSS_SELECTOR, ip)))   # 等待输入框出现
    for s in ['12FR010E-ND\n', 'CF1/4CT52R101J\n']:   # 循环字符串
      browser.switch_to.window(browser.current_window_handle)
      element = browser.find_element_by_css_selector(ip)
      element.send_keys(s)
      wait.until(expected.visibility_of_element_located((By.CSS_SELECTOR, rs)))
      result = browser.find_elements_by_css_selector(rs)
      for r in result:
            print(r.text)


if __name__ == "__main__":
    main()

grey_lemon 发表于 2021-5-31 11:28:22

suchocolate 发表于 2021-5-31 11:26


报错 selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

suchocolate 发表于 2021-5-31 11:31:45

grey_lemon 发表于 2021-5-31 11:28
报错 selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be...

把firefox换成你自己的chrome浏览器

suchocolate 发表于 2021-5-31 11:34:58

加个try,防止报错中断:from selenium import webdriver
from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support import expected_conditions as expected
from selenium.webdriver.support.wait import WebDriverWait


def main():
    browser = webdriver.Chrome(r'C:\Users\sesa395799\Downloads\chromedriver_win32\chromedriver.exe')
    ip = "#header > div.header__top > div.header__search > div > div.searchbox-inner > div.searchbox-inner-searchtext > input"
    rs = "#product-attributes > tbody > tr > td > div"
    browser.get('https://www.digikey.cn/zh/products/category/resistors/2')
    wait = WebDriverWait(browser, 30)
    wait.until(expected.visibility_of_element_located((By.CSS_SELECTOR, ip)))
    for s in ['12FR010E-ND\n', 'CF1/4CT52R101J\n']:
      browser.switch_to.window(browser.current_window_handle)
      element = browser.find_element_by_css_selector(ip)
      element.send_keys(s)
      wait.until(expected.visibility_of_element_located((By.CSS_SELECTOR, rs)))
      try:
            result = browser.find_elements_by_css_selector(rs)
            for r in result:
                print(r.text)
      except Exception as e:
            print(e)


if __name__ == "__main__":
    main()

grey_lemon 发表于 2021-5-31 11:35:51

报错:
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home


suchocolate 发表于 2021-5-31 11:57:45

grey_lemon 发表于 2021-5-31 11:35
报错:
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to b ...

系统CMD里运行如下代码,把chromedriver加入到path环境变量中。
PATH=%PATH%;C:\Users\sesa395799\Downloads\chromedriver_win32\

grey_lemon 发表于 2021-5-31 11:59:11

列表中增加至4个元素, 第三个查找不到, 报错, 没有继续查找第四个
Traceback (most recent call last):
File "C:\Users\sesa395799\PycharmProjects\pythonProject1\Selenium\test_fishC.py", line 31, in <module>
    main()
File "C:\Users\sesa395799\PycharmProjects\pythonProject1\Selenium\test_fishC.py", line 21, in main
    wait.until(expected.visibility_of_element_located((By.CSS_SELECTOR, rs)))
File "C:\Users\sesa395799\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium-3.141.0-py3.9.egg\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:




from selenium import webdriver
from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.suppor import expected_conditions as expected
from selenium.webdriver.support.wait import WebDriverWaitt


def main():
    browser = webdriver.Chrome(r'C:\Users\sesa395799\Downloads\chromedriver_win32\chromedriver.exe')
    ip = "#header > div.header__top > div.header__search > div > div.searchbox-inner > div.searchbox-inner-searchtext > input"
    rs = "#product-attributes > tbody > tr > td > div"
    browser.get('https://www.digikey.cn/zh/products/category/resistors/2')
    wait = WebDriverWait(browser, 30)
    wait.until(expected.visibility_of_element_located((By.CSS_SELECTOR, ip)))
    for s in ['12FR010E-ND\n', 'CF1/4CT52R101J\n','MR40MB12.70R001JT\n','RNF18FTD1K00\n']:
      browser.switch_to.window(browser.current_window_handle)
      element = browser.find_element_by_css_selector(ip)
      element.send_keys(s)
      wait.until(expected.visibility_of_element_located((By.CSS_SELECTOR, rs)))
      try:
            result = browser.find_elements_by_css_selector(rs)
            for r in result:
                print(r.text)
      except Exception as e:
            print(e)


if __name__ == "__main__":
    main()

suchocolate 发表于 2021-5-31 12:01:09

grey_lemon 发表于 2021-5-31 11:59
列表中增加至4个元素, 第三个查找不到, 报错, 没有继续查找第四个
Traceback (most recent call last): ...

把等待也加入到try里:from selenium import webdriver
from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support import expected_conditions as expected
from selenium.webdriver.support.wait import WebDriverWait


def main():
    browser = webdriver.Chrome(r'C:\Users\sesa395799\Downloads\chromedriver_win32\chromedriver.exe')
    ip = "#header > div.header__top > div.header__search > div > div.searchbox-inner > div.searchbox-inner-searchtext > input"
    rs = "#product-attributes > tbody > tr > td > div"
    browser.get('https://www.digikey.cn/zh/products/category/resistors/2')
    wait = WebDriverWait(browser, 30)
    wait.until(expected.visibility_of_element_located((By.CSS_SELECTOR, ip)))
    for s in ['12FR010E-ND\n', 'CF1/4CT52R101J\n', 'MR40MB12.70R001JT\n', 'RNF18FTD1K00\n']:
      browser.switch_to.window(browser.current_window_handle)
      element = browser.find_element_by_css_selector(ip)
      element.send_keys(s)
      try:
            wait.until(expected.visibility_of_element_located((By.CSS_SELECTOR, rs)))
            result = browser.find_elements_by_css_selector(rs)
            for r in result:
                print(r.text)
      except Exception as e:
            print(e)


if __name__ == "__main__":
    main()

grey_lemon 发表于 2021-5-31 12:12:13

成功了! 太感谢!
还有个问题, 这样打印出来的结果有些不整齐, 把结果打印成excel或者是列表形式的话要怎么设置?

感谢!

grey_lemon 发表于 2021-5-31 17:03:05

试了下把打印结果放到p_list[] 作为列表显示, 结果报错, AttributeError: 'list' object has no attribute 'text'。
麻烦看下要怎么修改?

from selenium import webdriver

wd = webdriver.Chrome(r'C:\Users\sesa395799\Downloads\chromedriver_win32\chromedriver.exe')

wd.get('https://www.digikey.cn/zh/products/category/resistors/2')
mainWindow = wd.current_window_handle

element = wd.find_element_by_css_selector("#header > div.header__top > div.header__search > div > div.searchbox-inner > div.searchbox-inner-searchtext > input")
list = ['12FR010E-N\n','MP930-15.0F-ND\n']

p_list = []

for i in :
    element.send_keys(list)

    p_list = wd.find_elements_by_css_selector('#product-attributes > tbody > tr > td > div')
    print(p_list)

    wd.switch_to.window(mainWindow)
    element = wd.find_element_by_css_selector("#header > div.header__top > div.header__search > div > div.searchbox-inner > div.searchbox-inner-searchtext > input")

wd.quit()

页: [1]
查看完整版本: Python web自动化 完善