gpt4 book ai didi

python - 如果按钮只有类型和值,如何使用 Python 在 Web 表单上提交数据

转载 作者:行者123 更新时间:2023-12-01 00:33:55 24 4
gpt4 key购买 nike

我有以下 URL,我想从中传递一些产品编号并获取当前 URL

https://www.skroutz.gr/

表格有

<form method="get" action="/search" accept-charset="utf-8">
<p>
<span class="search-bar-input-wrapper">
<input type="search" name="keyphrase" id="search-bar-input" value="" autocomplete="off" autocorrect="off" autocapitalize="off" placeholder="γράψε τον όρο αναζήτησης">
<span id="search-spinner" class="spinner"></span>
<span class="icon clear-search hidden"></span>
</span>
<button type="submit" value="submit">
Αναζήτηση
</button>
</p>
</form>

我使用以下内容提交搜索参数

records = []
listings =[]

Users = ['Mozilla/5.0 (Linux; <Android Version>; <Build Tag etc.>) AppleWebKit/<WebKit Rev>(KHTML, like Gecko) Chrome/<Chrome Rev> Safari/<WebKit Rev>',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36']
RandomAgent = np.random.choice(Users)
opts = Options()
opts.add_argument("user-agent=["+RandomAgent+"]")
br = webdriver.Chrome('C:\\chromedriver.exe',options=opts)


def current_time():
return datetime.datetime.now().strftime('%d-%m-%Y %H:%M')

def mycontents():
contents = []
with open('sku_list.csv', 'r',encoding='utf-8',newline='') as csvf:
reader = csv.reader(csvf, delimiter=";")
for row in reader:
contents.append(row)
return contents


def get_the_link(myskus):

br.get('https://www.skroutz.gr')
for item in myskus:
search_url = br.find_element_by_id('search-bar-input')
search_url.clear()
search_url.send_keys(item)
WebDriverWait(br,5).until(EC.element_to_be_clickable((By.XPATH,'//button[@value="submit"]'))).click()
records.append(br.current_url)
return records

但我收到错误,无法找到该元素

如何提交表格?

谢谢

最佳答案

引入 WebDriverWaitelement_to_be_clickable() 并在 xpath 下。

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

for item in myskus:

search_url = br.find_element_by_id('search-bar-input')
search_url.clear()
search_url.send_keys(item)
WebDriverWait(br,10).until(EC.element_to_be_clickable((By.XPATH,'//button[@value="submit"]'))).click()
records.append(br.current_url)
<小时/>

这是我尝试过的完整代码,它为我提供了输出。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
br=webdriver.Chrome()
br.get("https://www.skroutz.gr/")
myskus=['test','test1','abc']
records=[]
for item in myskus:

search_url = br.find_element_by_id('search-bar-input')
search_url.clear()
search_url.send_keys(item)
WebDriverWait(br,10).until(EC.element_to_be_clickable((By.XPATH,'//button[@value="submit"]'))).click()
records.append(br.current_url)

print(records)

输出:

['https://www.skroutz.gr/search?keyphrase=test', 'https://www.skroutz.gr/search?keyphrase=test1', 'https://www.skroutz.gr/search?keyphrase=abc']

关于python - 如果按钮只有类型和值,如何使用 Python 在 Web 表单上提交数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57954741/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com