我正在自动从气象机构下载数据。
问题是,在我努力使其更加独立的过程中,我试图使Selenium到Tab键指向某个位置,这样浏览器的焦点就可以"步行;到下载按钮。当我调用click((函数时,它什么也不做。因此,我尝试使用函数get_attribute("XPath"(提取XPath,但它返回None
。如何提取XPath?
我将把问题粘贴到这里:
Bandera=driver.find_element_by_xpath('/html/body/div[2]/div[2]/div/div[3]/div/div/div/div[1]/div/div/div[2]/div/table/tbody/tr[1]/td[1]/div/input')
Bandera.click()
Bandera.click()
## So Here i just select and dis-select a checkbox just to be near the Download button.
actions = ActionChains(driver)
actions.send_keys(Keys.TAB * 1 )
actions.perform()
#Here i just tabed to the button
Accion=driver.switch_to.active_element
#Maybe, here is when i lost the focus of the button?
Descarga_Actual=Accion.get_attribute("xpath")
谢谢你,很抱歉借用你的时间。
要点击悬停,我将使用以下序列:
your_dropdown_locator.click()
dropdown_option = driver.find_element_by_xpath("dropdown option locator")
actions = ActionChains(driver)
actions.move_to_element(dropdown_option)
actions.click().perform()
但是,这是通常用于下拉的方法。您使用:driver.find_element_by_xpath('/html/body/div[2]/div[2]/div/div[3]/div/div/div/div[1]/div/div/div[2]/div/table/tbody/tr[1]/td[1]/div/input')
这是您的代码的主要问题。如果你粘贴了这个下拉列表的html代码(或者你需要点击的按钮(,人们会帮助你找到更好的定位器。XPath/CSS必须是唯一的。在您的情况下,定位器非常糟糕。
此外,我认为两次制作Bandera.click()
毫无意义。
在你的情况下,据我所知,你只需要点击按钮。所以定位器是你的主要问题。你需要找到正确的定位器,等待按钮可点击,然后点击它。
你正在尝试做的另一个问题:
get_attribute("xpath")
看起来像是对get_attribute函数如何工作的错误预期。至少在这里检查这个函数的含义Python Selenium:使用xpath 查找对象属性