Python运行具有两个参数的JavaScript函数

2024-05-18 08:44:59 发布

您现在位置:Python中文网/ 问答频道 /正文

有一个链接

<a href="javascript:ga_and_go('//weathernews.jp/s/topics/?fm=onebox', 'topics_more')">…もっと見る</a> 

在html页面中,我想单击它,我尝试了如下代码,似乎页面无法解析,如何运行该函数ga_and_go('//weathernews.jp/s/topics/?fm=onebox', 'topics_more')

more_button = driver.find_element_by_partial_link_text("…もっと見る")
more_button.click()

Tags: andgo链接htmlmorebutton页面javascript
2条回答

根据HTML,元素似乎是基于JavaScript的,因此您需要归纳WebDriverWait,并可以使用以下任一解决方案:

  • 使用PARTIAL_LINK_TEXT

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "もっと見る"))).click()
    
  • 使用XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(.,'もっと見る')]"))).click()
    

注意:必须添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

可以尝试使用下面的xpath来单击元素吗?你知道吗

//a[contains(@href,'javascript:ga_and_go(')]

more_button = driver.find_element_by_xpath("//a[contains(@href,'javascript:ga_and_go(')]")
more_button.click()

相关问题 更多 >