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()
2
  • 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 Commented Jan 16, 2017 at 9: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. Commented Jan 16, 2017 at 9:14

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

6 Comments

Welcome. Mark this answer as "Accepted" if it solved your problem
Andersson, Thank you! One more question. Tell me please. How to make it work through a proxy in PhantomJS in the script?
Thank you! I made this worked with: service_args = [ '--proxy=78.23.244.145:80', '--proxy-type=http', ] driver = webdriver.PhantomJS(service_args=service_args)
Can i use list proxy? That there was a list of proxies and the script is worked on all proxies in the list?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.