【问题标题】:My script is unable to click on a certain grid我的脚本无法点击某个网格
【发布时间】:2018-07-15 07:45:09
【问题描述】:

我在 python 中结合 selenium 编写了一个脚本,以从网页获取一些信息。要获得内容,有必要跨几个步骤,如accepting conditionfill in the inputboxclick on the search button 来填充结果,最后在填充的表中单击the first grid(更具体地说,第一个 tr)。只要对the first tr 发起任何点击,就会打开一个新页面(包含所需信息)。

我的脚本可以成功完成前三个步骤。我不能做的是:

  1. perform a click on the first tr
  2. focus to the newly opened tab(包含我想要的信息)

要达到内容:

这是the link 关注。有一个接受按钮首先单击。然后有一个输入框Name要填写HMC DESIGN GROUP。现在,按下搜索按钮,结果应该出现在表格的下方。从那里我需要点击第一个 tr。

这是我迄今为止尝试过的:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
 
link = "https://officialrecords.broward.org/AcclaimWeb/search/SearchTypeName"

def get_information(driver,url):
    driver.get(url)
    wait.until(EC.element_to_be_clickable((By.ID, "btnButton"))).click()
    wait.until(EC.presence_of_element_located((By.ID,"SearchOnName"))).send_keys("HMC DESIGN GROUP")
    wait.until(EC.presence_of_element_located((By.ID, "btnSearch"))).click()
    wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,".t-grid-content table tr"))).click()

if __name__ == "__main__":
    driver = webdriver.Chrome()
    wait = WebDriverWait(driver,10)
    try:
        get_information(driver,link)
    finally:
        driver.quit()

目前脚本既没有点击网格the first tr of the newly generated table 也没有抛出任何错误。它优雅地退出浏览器。

【问题讨论】:

    标签: python python-3.x selenium selenium-webdriver web-scraping


    【解决方案1】:

    div 下还有另一个table,具有相同的类名"t-grid-content"。它显示为"Loading..."。因此,在您提交搜索后,您确实使用选择器 ".t-grid-content table tr" 进行了单击节点,但它只是没有产生适当的效果

    您只需要更具体的选择器。

    试一试

    wait.until(EC.element_to_be_clickable((By.XPATH, "//td[contains(., 'HMC DESIGN GROUP')]")))
    

    要切换到新窗口,请尝试将您的函数体更新为

    driver.get(url)
    current = driver.current_window_handle
    wait.until(EC.element_to_be_clickable((By.ID, "btnButton"))).click()
    wait.until(EC.presence_of_element_located((By.ID,"SearchOnName"))).send_keys("HMC DESIGN GROUP")
    wait.until(EC.presence_of_element_located((By.ID, "btnSearch"))).click()
    wait.until(EC.element_to_be_clickable((By.XPATH, "//td[contains(., 'HMC DESIGN GROUP')]"))).click()
    driver.switch_to.window([window for window in driver.window_handles if window != current][0])
    

    【讨论】:

    • 像魔术一样工作,先生。我的帖子中有另一个问题:如何切换到新打开的标签?非常感谢。
    • @Andersson :正如 OP 所说,他想点击第一个 tr ,所以点击 tr 的中间会很有意义,我们可以给他 css 选择器而不是 xpath。
    • @cruisepandey ,我不确定我明白你的意思。 "//td[contains(., 'HMC DESIGN GROUP')]" 从第一个 tr 返回表格单元格。
    • 没关系。但是您正在搜索名称列中寻找一个元素,对吗?我要说的是寻找完整的 tr 然后单击它的中间(尽管默认单击将在中间执行)。所以给他完整的 tr 行会很有意义 IMO。
    • 是的。完成新页面上所有必需的操作后,使用 driver.close() 关闭新窗口并使用 driver.switch_to.default_content() 返回主屏幕
    【解决方案2】:

    只需替换这个 css selctor

    .t-grid-content table tr 
    

    这个

    .t-grid-content table tr.t-state-selected:first-child  
    

    用于点击第一个 tr

    在单击第一个 tr 之前,将窗口句柄存储为:

    window_before = driver.window_handles[0]  
    

    点击第一个tr后,将新打开窗口的窗口句柄存储为:

    window_after = driver.window_handles[1]  
    

    现在你要做的就是将你的网络驱动程序的焦点切换到新打开的窗口。像这样:

    driver.switch_to_window(window_after)
    

    【讨论】:

    • 我尝试了(几次)你的选择器,但它无法启动点击。正如您已经知道的那样,当点击成功时,会打开一个新选项卡。
    猜你喜欢
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    相关资源
    最近更新 更多