我的脚本无法单击某个网格 [英] My script is unable to click on a certain grid

查看:60
本文介绍了我的脚本无法单击某个网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've written a script in python in combination with selenium to get some information from a webpage. To reach the content it is necessary to hurdle few steps, as in accepting condition, fill in the inputbox, click on the search button to populate results and finally click on the first grid (the first tr, more specifically) within the populated table. As soon as any click is initiated on the first tr, a new page (containing desired information) opens up.

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

My script can do the first three steps successfully. What I can't do are:

  1. perform a click on the first tr
  2. focus to the newly opened tab(包含我所关注的信息)
  1. perform a click on the first tr
  2. focus to the newly opened tab (containing information I'm after)

要获取内容:

这是链接.首先有一个接受按钮.然后有一个输入框NameHMC DESIGN GROUP填充.现在,按搜索按钮,结果应显示在表格的下方.从那里,我需要单击第一个tr.

This is the link to follow. There is an accept button to click first. Then there is an inputbox Name to be filled in with HMC DESIGN GROUP. Now, pressing the search button, the result should appear below within a table. From there I need to click on the first tr.

这是我到目前为止尝试过的:

This is what I've tried so far:

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,也不会引发任何错误.它会正常退出浏览器.

Currently the script neither clicks on the grid the first tr of the newly generated table nor throws any error. It quits the browser gracefully.

推荐答案

div下还有另一个table,具有相同的类名"t-grid-content".它显示为"Loading...".因此,在提交搜索之后,您确实可以使用选择器".t-grid-content table tr"单击节点,但效果不佳

There is another table under the div with the same class name "t-grid-content". It appears as "Loading...". So after you submit the search you really make a click on node with selector ".t-grid-content table tr", but it just don't make a proper effect

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

You just need more specific selector.

例如尝试

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

要切换到新窗口,请尝试将您的功能主体更新为

To switch to new window, try update your function body as

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])

这篇关于我的脚本无法单击某个网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆