如何使用Python Selenium和send_keys方法在fbref.com中生成搜索结果将文本发送到搜索字段
当使用send_keys和selenium中的execute_script使用Chrome Web驱动程序时,我无法在fbref.com中检索任何搜索
。
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import pandas as pd
import numpy as np
import csv
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.action_chains import ActionChains
s=Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s)
driver.get("https://fbref.com/en/")
element = driver.find_element(by=By.CLASS_NAME, value="ac-hint")
action = ActionChains(driver)
element1= driver.find_element(by=By.CSS_SELECTOR, value=("input[type='search']"))
action.click(on_element=element1)
action.perform()
#element.send_keys("lionel messi")
#driver.execute_script("arguments[0].value='lionel messi'",element)
element2=driver.find_element(by=By.CSS_SELECTOR, value=("input[type='submit']"))
action.click(on_element=element2)
action.perform()```
结果 键入并单击搜索按钮没有任何麻烦,但是搜索结果如下:
I am unable to retrieve any search results in fbref.com when using either of send_keys and execute_script in selenium for python using chrome web driver
This is the code ive used so far:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import pandas as pd
import numpy as np
import csv
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.action_chains import ActionChains
s=Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s)
driver.get("https://fbref.com/en/")
element = driver.find_element(by=By.CLASS_NAME, value="ac-hint")
action = ActionChains(driver)
element1= driver.find_element(by=By.CSS_SELECTOR, value=("input[type='search']"))
action.click(on_element=element1)
action.perform()
#element.send_keys("lionel messi")
#driver.execute_script("arguments[0].value='lionel messi'",element)
element2=driver.find_element(by=By.CSS_SELECTOR, value=("input[type='submit']"))
action.click(on_element=element2)
action.perform()```
The code is able to interact with the search button and the text is typed and the search button is clicked without any trouble but the search result is as follows:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在错误的字段中执行播放器名称输入,如果您仔细查看HTML,则有2个输入字段用于搜索。
而不是“ ac Hint”,而是使用“交流输入”:
You are doing your player name input in the wrong field, if you look closely at the html, there are 2 input fields for the search.
instead of the "ac-hint", use "ac-input":
定位器策略您已经用来识别搜索字段 em>
在
解决方案
,以将a 字符序列发送到您需要诱导 webdriverwait 对于您可以使用以下两个解决方案:
代码块:
注意:您必须添加以下导入:
浏览器快照:
The locator strategy you have used to identify the search field
doesn't identifies the desired element uniquely within the HTML DOM
Solution
To send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solution:
Code Block:
Note: You have to add the following imports :
Browser Snapshot: