ballade4op52
ballade4op52

Reputation: 2267

How can these javascript links be traversed with Selenium or BeautifulSoup?

I’ve opted for Selenium since the links’ hrefs are generated dynamically, although some method via bs4 would be preferred.

I'm using PhantomJS but have tried Firefox as well

When attempting to click a link, nothing happens.

For instance,

url = 'http://www.achema.de/de/ausstellung/aussteller-und-produkte.html'

driver.get(url)

resultsBox = driver.find_element_by_css_selector('div[id="ix_result"]')

for tr in resultsBox.find_elements_by_tag_name('tr'):
    link = tr.find_element_by_tag_name('a')
    link.click()

    # I've also tried:
    # ActionChains(driver).move_to_element(link).click(link).perform()

Upvotes: 1

Views: 65

Answers (2)

Jeremy
Jeremy

Reputation: 394

I'm not quite sure I know what you're trying to click but I clicked the first listing on the page "Agora Pavillon C7" with the following code.

from selenium import webdriver

def so_test():

    driver = webdriver.Firefox()

    def connect():
        driver.get('http://www.achema.de/de/ausstellung/aussteller-und-produkte.html')
        div = driver.find_element_by_id('ix_result_aussteller')
        test_link = div.find_elements_by_tag_name('tr')
        link = test_link[0].find_element_by_tag_name('a')
        link.click()

    connect()

so_test()

If you are trying to click the navigation links above, the proper target would be something like:

from selenium import webdriver

def so_test():

    driver = webdriver.Firefox()

    def connect():
        driver.get('http://www.achema.de/de/ausstellung/aussteller-und-produkte.html')
        div = driver.find_element_by_id('ix_letters')
        test_link = div.find_elements_by_tag_name('li')
        link = test_link[0].find_element_by_tag_name('a')
        link.click()

    connect()

so_test()

Something like that. Let me know if I'm off base. I'd be glad to try and help further.

Upvotes: 1

ballade4op52
ballade4op52

Reputation: 2267

As Orenthal indicated, I found that the link was being clicked. However, the page to be loaded was completely dynamic, so a sleep of at least 2 seconds needed to occur before attempting to extract from that link.

Upvotes: 2

Related Questions