简体   繁体   English

Python 用两个参数运行 JavaScript 函数

[英]Python to run JavaScript function with two parameters

There is a link有一个链接

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

in a html page, I want to click it, I tried code as below, it seems like page can't be parsed, how can I run that function ga_and_go('//weathernews.jp/s/topics/?fm=onebox', 'topics_more') :在html页面中,我想单击它,我尝试了如下代码,似乎无法解析页面,我该如何运行该函数ga_and_go('//weathernews.jp/s/topics/?fm=onebox', 'topics_more') :

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

Can you try using the below xpath to click the element?您可以尝试使用以下 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()

As per the HTML the element seems to be JavaScript based so you need to induce WebDriverWait and can use either of the following solutions:根据 HTML,该元素似乎是基于JavaScript的,因此您需要引入WebDriverWait并可以使用以下任一解决方案:

  • Using PARTIAL_LINK_TEXT :使用PARTIAL_LINK_TEXT

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

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

Note : You have to add the following imports :注意:您必须添加以下导入:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM