无法使用selenium从dropdownlist命名的pro列表中选择CS:GO

2024-05-15 01:26:12 发布

您现在位置:Python中文网/ 问答频道 /正文

有一个表是动态的,要获取该表,需要从名为PRO LISTS的dropdownlist中单击CS:GO。我试过了,但无法选择正确的定位器。谢谢

URL

我尝试了以下方法:

#driver.find_element_by_xpath('(//i[@class="x-anchor-sub-indicator"])[1]').click()
        #time.sleep(5)
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '(//*[@class="sub-menu x-dropdown x-active"]/li/a)[1]'))).click()
        driver.execute_script("arguments[0].click();", driver.find_element_by_xpath('(.//*[@class="sub-menu x-dropdown x-active"]/li/a)[1]/div/div/span'))

更新:

import scrapy
from scrapy.selector import Selector
from scrapy_selenium import SeleniumRequest
from selenium.webdriver.common.keys import Keys
import time

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


class DropdownScrollTable(scrapy.Spider):
    name = 'dst'

    def start_requests(self):
        yield SeleniumRequest(
            url='https://prosettings.net/cs-go-pro-settings-gear-list/',
            wait_time=5,
            screenshot=True,
            callback=self.parse)

    def parse(self, response):

        driver = response.meta['driver']
        wait = WebDriverWait(driver, 20)
        ActionChains(driver).move_to_element(wait.until(EC.visibility_of_element_located((By.XPATH, "//span[contains(text(),'Pro Lists')]/ancestor::a")))).pause(5).perform()
        wait.until(EC.visibility_of_element_located((By.XPATH, "//span[contains(text(),'CS:GO')]/ancestor::a"))).click()
        
        driver.save_screenshot('search_result.png')

Tags: fromimportbytimedriverseleniumelementclass
1条回答
网友
1楼 · 发布于 2024-05-15 01:26:12

需要记录的事项:

  1. 我们需要在专业列表上悬停,所以我们将使用ActionChains
  2. 我们一悬停,就会看到一个选项列表,第一个元素是CS:GO
  3. 以全屏模式启动浏览器
  4. 使用显式等待让web元素正确呈现

代码

driver = webdriver.Chrome(driver_path)
driver.maximize_window()
#driver.implicitly_wait(50)
driver.get("https://prosettings.net/cs-go-pro-settings-gear-list/");
wait = WebDriverWait(driver, 20)
ActionChains(driver).move_to_element(wait.until(EC.visibility_of_element_located((By.XPATH, "//span[contains(text(),'Pro Lists')]/ancestor::a")))).pause(5).perform()
wait.until(EC.visibility_of_element_located((By.XPATH, "//span[contains(text(),'CS:GO')]/ancestor::a"))).click()

导入:

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

相关问题 更多 >

    热门问题