簡體   English   中英

如何使用 Python 從 Selenium 中的列表中輸入變量文本?

[英]How to input variable text from list in Selenium with Python?

我正在尋找一種從關鍵字列表中輸入 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()

像這樣的東西應該適用於搜索關鍵字列表。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM