python selenium:如何获取第一个隐藏按钮的 url?
python selenium: how to get first hidden button's url?
我处理的页面有不可见的隐藏选项按钮。
* download sample video in the page(button is hidden by html)
[button1] (<- LINK_TEXT i s 'button1')
[button2]
[button3]
所以,我用了'EC.element_to_be_clickable'。
此代码有效,但如果我不知道按钮的 LINK_TEXT,则无法使用这种方式。 LINK_TEXT 每个页面的名称都不同。
我只想获取视频的第一个 link url(ex- button1)。
_sDriver = webdriver.Firefox()
_sDriver.get('www.example.com/video')
wait = WebDriverWait(_sDriver, 10)
download_menu = _sDriver.find_element_by_id("download-button")
action = ActionChains(_sDriver)
action.move_to_element(download_menu).perform()
documents_url = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "button1"))).get_attribute('href')
我的代码的结果是由 'button1' 的 url 获得的,但是如果我不知道 'button1' 文本,如何使用 url 获得第一个隐藏按钮=29=]?
感谢您的帮助。
首先,"button" 我假设您在这种情况下指的是 a
元素。
而且,由于按钮是隐藏的,element_to_be_clickable
将不起作用,请使用 presence_of_element_located
。要获取第一个 a
元素,请使用 "by tag name" 定位器:
documents_url = wait.until(EC.presence_of_element_located((By.TAG_NAME, "a"))).get_attribute('href')
可能有更好的方法来定位元素,如果没有看到提到的 "button" 元素的实际 HTML,很难判断。
我处理的页面有不可见的隐藏选项按钮。
* download sample video in the page(button is hidden by html)
[button1] (<- LINK_TEXT i s 'button1')
[button2]
[button3]
所以,我用了'EC.element_to_be_clickable'。 此代码有效,但如果我不知道按钮的 LINK_TEXT,则无法使用这种方式。 LINK_TEXT 每个页面的名称都不同。
我只想获取视频的第一个 link url(ex- button1)。
_sDriver = webdriver.Firefox()
_sDriver.get('www.example.com/video')
wait = WebDriverWait(_sDriver, 10)
download_menu = _sDriver.find_element_by_id("download-button")
action = ActionChains(_sDriver)
action.move_to_element(download_menu).perform()
documents_url = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "button1"))).get_attribute('href')
我的代码的结果是由 'button1' 的 url 获得的,但是如果我不知道 'button1' 文本,如何使用 url 获得第一个隐藏按钮=29=]?
感谢您的帮助。
首先,"button" 我假设您在这种情况下指的是 a
元素。
而且,由于按钮是隐藏的,element_to_be_clickable
将不起作用,请使用 presence_of_element_located
。要获取第一个 a
元素,请使用 "by tag name" 定位器:
documents_url = wait.until(EC.presence_of_element_located((By.TAG_NAME, "a"))).get_attribute('href')
可能有更好的方法来定位元素,如果没有看到提到的 "button" 元素的实际 HTML,很难判断。