1

I am trying to use Selenium in Python, as as I am a beginner in doing so I cannot get send_key to work, but most probably it is straight forward and I am missting something.

Here is an example of what I have done so far:

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("https://semantria.com/demo")
item = driver.find_element_by_id("analyze_url_form")
item.send_keys("http://finance.yahoo.com/news/skystar-bio-pharmaceutical-company-provides-133000048.html")
go_button = driver.find_element_by_id("analyze_url_button")
go_button.click()

The idea is that in the https://semantria.com/demo website, there is an empty space that one can enter a website link, and then click on the Go button. However, it looks like my code does not do this.

Am I doing something wrong? Does this website do something that I should be aware of and change my code accordingly? Any help on this is really appreciated.

1 Answer 1

3

The problem is that you are sending keys to the form element, not the input element inside.

Plus, you can just send the URL with a new line at the end which is the same as you've entered the URL and pressed ENTER key which results in the form being submitted. Works for me:

item = driver.find_element_by_css_selector("form#analyze_url_form input[name=link]")
item.send_keys("http://finance.yahoo.com/news/skystar-bio-pharmaceutical-company-provides-133000048.html" + "\n")

As a bonus, here is how you can grab the sentiment value (you have to let selenium know what to wait for via WebDriverWait and Expected Conditions):

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


driver = webdriver.Firefox()
driver.get("https://semantria.com/demo")

item = driver.find_element_by_css_selector("form#analyze_url_form input[name=link]")
item.send_keys("http://finance.yahoo.com/news/skystar-bio-pharmaceutical-company-provides-133000048.html" + "\n")

wait = WebDriverWait(driver, 30)

sentiment_value = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "strong.sentiment_score_value")))
print(sentiment_value.text)

Prints positive (+0.230).

Sign up to request clarification or add additional context in comments.

7 Comments

Fantastic, it worked nicely. Can you please tell me now how to look at the content that webpage returns?
thanks. Where is the whole content of webpage not just the sentiment value?
@TJ1 if you need the complete content, right after the wait is done (meaning the results are now loaded), get the driver.page_source. Hope that helps.
Thanks alecxe yes that was very helpful I really appreciate it.
Thanks alecxe. Here is the new question: stackoverflow.com/questions/33062149/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.