在使用硒进行网络搜刮时,如何清除搜索区域?

问题描述 投票:0回答:1
from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.common.keys import Keys
import datetime
import pandas as pd
import requests


driver = webdriver.Chrome('./chromedriver')
driver.set_window_size(800,600)
driver.implicitly_wait(5)
driver.get('https://news.google.com/?hl=ko&gl=KR&ceid=KR%3Ako')
driver.implicitly_wait(2)

words = ['atom','mechanic','clock']

for word in words:

    if datetime.datetime.today()==0:
        word = word+'when:3d'
    else:
        word = word +'when:1d'

    elem = driver.find_element_by_xpath('//*[@id="gb"]/div[2]/div[2]/div/form/div[1]/div/div/div/div/div[1]/input[2]')
    elem.send_keys(Keys.CONTROL + 'a' + Keys.NULL, word.decode('utf-8'))

    elem.send_keys(Keys.RETURN)

以上是整个网页刮痧的脚本,其他的都能正常使用,但这部分。elem.send_keys(Keys.CONTROL + 'a' + Keys.NULL, word.decode('utf-8')),没有任何作用。有什么小窍门可以解决这个问题,清除下一个词的搜索区域?

python selenium textarea webdriverwait clear
1个回答
0
投票

当使用 你要诱导 WebDriverWait 对于element_to_be_clickable() 您可以使用以下方法 定位器策略:

  • 使用 XPATH:

    element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='gb']/div[2]/div[2]/div/form/div[1]/div/div/div/div/div[1]/input[2]")))
    element.click()
    element.clear()
    
  • 说明: : 你必须增加以下进口:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
© www.soinside.com 2019 - 2024. All rights reserved.