2

I have a problem:

Here is a code to click the link on the site in Firefox. It works. Clicks. But the same code in PhantomJS going to a page but not clicks. 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()
Antony Hatchkins
  • 31,947
  • 10
  • 111
  • 111
  • 1
    How do you know that there there was no click? It's a headless browser. At least you should add `browser.get_screenshot_as_file(path_to_file)` to check whether page changed or not – Andersson Jan 16 '17 at 09:05
  • I missed this line in the code. I use it. With "browser.get_screenshot" and I know that he is not clicked. Just loaded page, but not completed following the link. – Andrew Foxis Jan 16 '17 at 09:14

1 Answers1

2

The link that you're trying to click has attribute target="_blank" which means that this link should be opened in new tab (window). 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()
Andersson
  • 51,635
  • 17
  • 77
  • 129