如何在selenium python中搜索并选择网站中的第一个选项



我有这样的代码https://xiaomifirmwareupdater.com/miui/,搜索查询并选择第一个元素并下载rom,但它总是选择同一个元素,尽管查询不同,我最初以为网站给出了相同的顶部结果,但我用浏览器检查了一下,但它给出了不同的结果,我该如何修复/做这件事?

我的代码:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options
import asyncio
GOOGLE_CHROME_BIN = 'path here'
CHROME_DRIVER = 'driver path here'
async def bruh(query="Redmi Note 8 Pro China"):
url = "https://xiaomifirmwareupdater.com/miui"
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.binary_location = GOOGLE_CHROME_BIN
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-gpu")
driver = webdriver.Chrome(executable_path=CHROME_DRIVER, options=chrome_options)
driver.get(url)
await asyncio.sleep(10)
w = WebDriverWait(driver, 20)
search_xpath = '/html/body/div[3]/section/div[2]/div[3]/div[1]/div/div[2]/div[1]/div[2]/div/label/input'
next_page_url_xpath = '/html/body/div[3]/section/div[2]/div[3]/div[1]/div/div[2]/div[2]/div/table/tbody/tr[1]/td[8]/a'
version_xpath = '/html/body/div[3]/section/div[2]/div[2]/div[2]/div[1]/div/ul/li[3]/h5'
name_xpath = '/html/body/div[3]/section/div[2]/div[2]/div[2]/div[1]/div/ul/li[8]/h5/span'
w.until(expected_conditions.presence_of_element_located((By.XPATH, search_xpath)))
elem = driver.find_element_by_xpath(search_xpath)
elem.send_keys(query)
await asyncio.sleep(20)
next_page_elem = driver.find_element_by_xpath(next_page_url_xpath)
nextm = next_page_elem.get_attribute('href')
driver.get(nextm)
await asyncio.sleep(10)
version_elem = driver.find_element_by_xpath(version_xpath).text
name_elem = driver.find_element_by_xpath(name_xpath).text
version_elem = version_elem.replace("Version: ", "")
print(version_elem)
print(name_elem)
url = f"https://bigota.d.miui.com/{version_elem}/{name_elem}"
print(url)
driver.close()

我想访问网站,发送我的查询,选择第一个选项,并将其转换为可下载的url。有人能帮忙吗?谢谢

你正在做很多我不太了解的额外事情。

一些反馈。。。

  1. 等待存在只是意味着元素在DOM中,而不是它是可见的或可交互的。如果您要使用点击或发送密钥,则需要分别等待可点击或可见,否则可能会出现异常
  2. 你不需要所有这些睡眠,尤其是当你使用WebDriverWait时。最佳做法是避免睡眠(它们会使脚本变得更慢、更不可靠(,而是使用WebDriverWait
  3. 等待元素会返回该元素,因此您不需要等待,然后查找,然后单击。。。您可以只使用wait.until(...).click()
  4. 您正在从链接获取href,然后导航到它…只需单击该链接
  5. 我只是用了split()而不是replace()。。。两者都可以。我认为split()不太可能破裂,例如,如果他们更换标签

根据我上面的反馈更新您的代码,这应该会起作用。

driver.get(url)
wait = WebDriverWait(driver, 20)
wait.until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, "input"))).send_keys(query)
wait.until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR, "#miui td > a"))).click()
version = wait.until(expected_conditions.visibility_of_element_located((By.XPATH, "//h5[./b[text()='Version: ']]"))).text.split()[1]
package_name = wait.until(expected_conditions.visibility_of_element_located((By.ID, "filename"))).text
print(version)
print(package_name)
url = f"https://bigota.d.miui.com/{version}/{package_name}"
print(url)
driver.close()

相关内容

  • 没有找到相关文章

最新更新