简体   繁体   English

Python+Selenium+PhantomJs = 没有点击 =( 但是 Firefox 点击

[英]Python+Selenium+PhantomJs = Not clicking =( But Firefox clicks

I have a problem:我有个问题:

Here is a code to click the link on the site in Firefox.这是在 Firefox 中单击站点上的链接的代码。 It works.有用。 Clicks.点击次数。 But the same code in PhantomJS going to a page but not clicks.但是 PhantomJS 中的相同代码转到页面但不点击。 Please help solve the problem.请帮助解决问题。 thanks in advance提前致谢

from selenium import webdriver
import time
browser=webdriver.PhantomJS()
browser.get('http://nnmclub.to')
time.sleep(10)
browser.find_element_by_xpath("//a[contains(@href,'www.marketgid.com')]").click()
time.sleep(10)
browser.quit()

The link that you're trying to click has attribute target="_blank" which means that this link should be opened in new tab (window).您尝试单击的链接具有属性target="_blank"这意味着该链接应该在新选项卡(窗口)中打开。 To see that it actually clicked you should try to switch to that new window with following code:要查看它是否真的点击了,您应该尝试使用以下代码切换到该新窗口:

from selenium import webdriver
import time

browser=webdriver.PhantomJS()
browser.get('http://nnmclub.to')
current = browser.window_handles[0]
time.sleep(10)
browser.find_element_by_xpath("//a[contains(@href,'www.marketgid.com')]").click()
time.sleep(10)
newWindow = [window for window in browser.window_handles if window != current][0]
browser.switch_to.window(newWindow)
browser.get_screenshot_as_file(path_to_file)
browser.quit()

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

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