如何使用Python Selenium和send_keys方法在fbref.com中生成搜索结果将文本发送到搜索字段

发布于 2025-01-22 23:03:36 字数 1305 浏览 0 评论 0原文

当使用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:

which basically means that the search was invalid ,ive tried to search manually in the browser window opened by the driver and that gives me a successful result

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对风讲故事 2025-01-29 23:03:36

您正在错误的字段中执行播放器名称输入,如果您仔细查看HTML,则有2个输入字段用于搜索。
而不是“ ac Hint”,而是使用“交流输入”:

element = driver.find_element(by=By.CLASS_NAME, value="ac-input")

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":

element = driver.find_element(by=By.CLASS_NAME, value="ac-input")
暗地喜欢 2025-01-29 23:03:36

定位器策略您已经用来识别搜索字段 em>


解决方案

,以将a 字符序列发送到您需要诱导 webdriverwait 对于您可以使用以下两个解决方案:

  • 代码块:

      driver.get(“ https://fbref.com/en/”)
    webdriverwait(驱动程序,20).unifor(ec.element_to_be_be_clickable(((by.css_selector Messi” + keys.turn)
     
  • 注意:您必须添加以下导入:

     来自selenium.webdriver.common.keys导入键
    来自selenium.webdriver.support.ui导入WebDriverWait
    从selenium.webdriver.common.通过进口
    从selenium.webdriver.support进口预期_conditions作为ec
     
  • 浏览器快照:

“

The locator strategy you have used to identify the search field
doesn't identifies the desired element uniquely within the HTML DOM

4elements


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:

    driver.get("https://fbref.com/en/")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[type='search'][placeholder='Enter Person, Team, Section, etc']"))).send_keys("lionel messi" + Keys.RETURN)
    
  • Note: You have to add the following imports :

    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

lionel messi

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文