0

I got stuck with extracting href="/ttt/play" from the following HTML code.

<div class="collection-list-wrapper-2 w-dyn-list">
  <div class="w-dyn-items">
    <div typeof="ListItem" class="collection-item-2 w-clearfix w-dyn-item">
       <div class="div-block-38 w-hidden-medium w-hidden-small w-hidden-tiny"><a href="https://ttt.io/" target="_blank" property="sameAs" class="link-block-5 w-inline-block"><img src="https://global-uploads.webflow.com/59cf_home.svg" width="16" height="16" alt="Official Link" class="image-28"></a>

       <a property="url" href="/ttt/play" class="link-block-4 w-inline-block">
        <div class="row-7 w-row"><div class="column-10 w-col w-col-2"><img height="25" property="image" src="https://global-fb0edc0001b4b11d/5a77ba9773fd490001ddaaaa_play.png" alt="Play" class="image-23"><h2 property="name" class="heading-34">Play</h2><div style="background-color:#d4af37;color:white" class="text-block-28">GOLD LEVEL</div><div class="text-block-30">HOT</div><div style="background-color:#d4af37;color:white" class="text-block-28 w-condition-invisible">SILVER LEVEL</div></div></div></a>
        </div>

    <div typeof="ListItem" class="collection-item-2 w-clearfix w-dyn-item">

This is my code in Python:

driver = webdriver.PhantomJS()
driver.implicitly_wait(20)
driver.set_window_size(1120, 550)
driver.get(website_url)
tag = driver.find_elements_by_class_name("w-dyn-item")[0]
tag.find_element_by_tag_name("a").click()
url = driver.current_url
print(url)
driver.quit()

When I print url using print(url), I want to see url equal to website_url/ttt/play, but instead of it I get website_url. It looks like the click event does not work and the new link is not really opened.

Markus
  • 3,562
  • 12
  • 48
  • 85
  • You need to wait for the new page to load. I also suggest that you use the mozilla or chrome WebDriver so that you can see what your code is doing. – Code-Apprentice Feb 13 '18 at 15:01
  • Are you trying to just extract the `href` or actually follow the link and get the URL? – JeffC Feb 13 '18 at 16:51
  • 1
    @JeffC: Are you downvoting the questions of others, because yours are downvoted by so many people? – Markus Feb 13 '18 at 17:26

1 Answers1

0

When using .click() it must be "visible" (you using PhantomJS) and not hidden, in a drop-down for example.
Also make sure the page is completely loaded.

As i see it you have two options:

  1. Ether use selenium to revile it, and then click.
  2. Use java script to do the actual click

I strongly suggest to click with javascript, its much faster and more reliable.

Here is a little wrapper to make things easier:

def execute_script(driver, xpath):
    """ wrapper for selenium driver execute_script
    :param driver: selenium driver
    :param xpath: (str) xpath to the element
    :return:  execute_script result
    """
    execute_string = "window.document.evaluate('{}', document, null, 9, null).singleNodeValue.click();".format(xpath)

    return driver.execute_script(execute_string)

The wrapper basically implement this technique to click on elements with javascript.

then in your selenium script use the wrapper like so:

execute_script(driver, element_xpath)

you can also make it more general to not only do clicks, but scrolls and other magic..

ps. in my example i use xpath, but you can also use css_path basically, what-ever runs in javascript.

Urban48
  • 1,398
  • 1
  • 13
  • 26
  • Thanks. Could you please elaborate a little bit the wrapper. What do you mean by `window.document.evaluate('{}', document, null, 9, null).singleNodeValue.click()`? How should I use it in my particular case? – Markus Feb 13 '18 at 15:49
  • This is the link that I am working with: https://topicolist.com/ongoing-ico I need to click on each project and enter into its page. – Markus Feb 13 '18 at 15:50
  • @Markus the execute string is just a javascript command that u can run in a browser console. to figure out what javascript script you can execute just play with the developer tools console in your browser and try to click your element with a javascript command – Urban48 Feb 13 '18 at 16:01
  • @Markus for example to click on the first project there u will need to run window.document.evaluate('/html/body/div[3]/div[1]/div[1]/div[1]/div/div[1]/a/div[1]/div[2]', document, null, 9, null).singleNodeValue.click(); you can do the same from python with selenium with the code example i provided – Urban48 Feb 13 '18 at 16:02
  • Og, I got it. When I run `driver.quit()` after this code, I get the error `Bad file descriptor`. Do you know how to deal with it? – Markus Feb 13 '18 at 16:19
  • @Markus, hard to say without seeing all your code. If you found my answer helpful please consider accepting it – Urban48 Feb 13 '18 at 16:26
  • I am using your approach and get one more question: https://stackoverflow.com/questions/48772798/webdriverexception-message-errormessagenull-is-not-an-object – Markus Feb 13 '18 at 18:11