使用 Selenium 单击“Resgatar”按钮直到昨天才起作用。网站是:https://www.tesourodireto.com.br/titulos/precos-e-taxas.htm.
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
options = Options()
options.headless = True
browser = webdriver.Chrome('/Users/alvaro/downloads/chromedriver2',options=options)
browser.get('https://www.tesourodireto.com.br/titulos/precos-e-taxas.htm')
link = browser.find_element(By.PARTIAL_LINK_TEXT,"Resgatar")
browser.execute_script("arguments[0].click();", link)
我尝试过WebDriverWait,但没有解决问题。 By.CLASS_NAME 都不是。有什么想法吗?
这是一个巨大的html,超过5k行。在pastebin上查看:https://pastebin.com/vBD1mVd2。这是按钮周围的代码:
<div class="td-precosTaxas-wrapper">
<div class="td-tabs td-precosTaxas-tabs">
<div class="td-precosTaxas-tabs__content">
<ul class="td-tabs-precos_taxas-links td-tabs-links td-internal-wrapper--no-mobile" data-tabgroup="td-precos_taxas-tab-group">
<li class="td-tabs-links__item">
<a class="td-tabs-links__link active" role="button" href="#td-precos_taxas-tab_1">
Investir
</a>
</li>
<li class="td-tabs-links__item">
<a class="td-tabs-links__link td-link-tab-2" role="button" href="#td-precos_taxas-tab_2">
Resgatar
</a>
</li>
</ul>
尝试使用:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
btn = wait.until(EC.element_to_be_clickable((By.XPATH, '//a[@href="#td-precos_taxas-tab_2"]')))
browser.execute_script("arguments[0].click();", btn)
或者你可以使用
ActionChains
from selenium.webdriver.common.action_chains import ActionChains
btn = wait.until(EC.element_to_be_clickable((By.XPATH, '//a[@href="#td-precos_taxas-tab_2"]')))
actions = ActionChains(browser)
actions.move_to_element(btn).click().perform()
希望这会有所帮助。