【问题标题】:How to input variable text from list in Selenium with Python?如何使用 Python 从 Selenium 中的列表中输入变量文本?
【发布时间】:2019-01-08 21:30:14
【问题描述】:

我正在寻找一种从关键字列表中输入 Google 研究的方法。

它将作为循环工作,循环 1 = "keyword1",循环 2 = "keyword2"...

这里是循环代码:

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
import time

driver = webdriver.Chrome()

def main():

    driver.get('https://www.google.com')

    driver.find_element_by_xpath('//*[@id="tsf"]/div[2]/div/div[1]/div/div[1]/input').send_keys('keyword1')
    driver.find_element_by_xpath('//*[@id="tsf"]/div[2]/div/div[1]/div/div[1]/input').send_keys(Keys.ENTER)

    time.sleep(2)

main()

while True:
    main()

【问题讨论】:

    标签: python python-3.x selenium variables automation


    【解决方案1】:

    这样的东西应该适用于搜索关键字列表。

    from selenium import webdriver
    from selenium.common.exceptions import TimeoutException
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.keys import Keys
    import time
    
    driver = webdriver.Chrome()
    keyword_list = ["keyword1", "keyword2", "keyword3"] # Your list of keywords
    
    def googleSearch(keyword):
        ''' This function searches Google for the given keyword '''
        driver.get('https://www.google.com')
    
        driver.find_element_by_xpath('//*[@id="tsf"]/div[2]/div/div[1]/div/div[1]/input').send_keys(keyword)
        driver.find_element_by_xpath('//*[@id="tsf"]/div[2]/div/div[1]/div/div[1]/input').send_keys(Keys.ENTER)
    
        time.sleep(2)
    
    for keyword in keyword_list: # This runs thru your keywordlist keyword by keyword
        googleSearch(keyword) # and this run the function to search for the keyword
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-04
      • 1970-01-01
      • 2017-03-21
      • 2014-06-29
      相关资源
      最近更新 更多