Python使用两个参数运行JavaScript函数
有一个链接Python使用两个参数运行JavaScript函数,javascript,python,selenium,beautifulsoup,webdriverwait,Javascript,Python,Selenium,Beautifulsoup,Webdriverwait,有一个链接 <a href="javascript:ga_and_go('//weathernews.jp/s/topics/?fm=onebox', 'topics_more')">…もっと見る</a> 可以尝试使用下面的xpath来单击元素吗 //a[包含(@href,'javascript:ga_和_go(')] 根据HTML,该元素似乎是基于JavaScript的,因此您需要引入WebDriverWait,并可以使用以下任一解决方案: 使用部分链接文本: W
<a href="javascript:ga_and_go('//weathernews.jp/s/topics/?fm=onebox', 'topics_more')">…もっと見る</a>
可以尝试使用下面的xpath来单击元素吗
//a[包含(@href,'javascript:ga_和_go(')]
根据HTML,该元素似乎是基于JavaScript的,因此您需要引入WebDriverWait,并可以使用以下任一解决方案:
- 使用部分链接文本:
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
您是否收到任何错误?或者它只是没有做任何事情?这与
driver.execute_脚本(“ga_and_go('//weathernews.jp/s/topics/?fm=onebox',topics_more')相同吗
?不,页面的javascript将以一种你无法模仿的方式使用,除非你有权访问底层的javascript库,但我在执行脚本后得到了我所需要的。我得到了错误SyntaxError:未能在“Document”上执行“evaluate”:字符串“//a[包含(@href,'javascript:ga_和_go('//weathernews.jp/s/topics')]'不是有效的XPath表达式。
是的,明白了。您应该对第二个撇号进行注释。但是,您可以使用此XPath://a[contains(@href,'javascript:ga_and_go(')]@mikezang,您是否有机会尝试使用此XPath:://a[contains(@href,'javascript:ga_and_go(')]
?
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