从网页发送和接收数据-Selenium [英] Sending and Receive data from a web page - Selenium

查看:256
本文介绍了从网页发送和接收数据-Selenium的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:

我正在使用硒python绑定将一个字(此网站)发送到网页执行一些处理(将包含单词的单词分隔成单词),并通过以下方式向用户显示结果:

单词:竞争

结果:单词竞争"分成塞拉贝斯

在检查元素工具中,我一直在检查输入文本的id属性

从下面的python脚本到selenium Web驱动程序API,我正在向搜索输入文本发送一些单词,然后也按Enter键以执行操作.

# Currently supported WebDriver implementations are Firefox, Chrome, Ie and Remote
from selenium import webdriver

# Interacting with the RETURN KEY
from selenium.webdriver.common.keys import Keys

# Creating a web driver firefox instance
driver = webdriver.Firefox()

# With the get method we go to the webpage in the url given
driver.get("http://tip.iatext.ulpgc.es/silabas/Default.aspx")

# Assertion that checks if the word "Silabeador" is in the title webpage
assert "Silabeador" in driver.title

# WebDriver let me interact with items in a web page through of their attributtes
# More information http://selenium-python.readthedocs.org/locating-elements.html#locating-elements
elem = driver.find_element_by_id("MainContent_TextBox1")
elem.send_keys("Competencia")
elem.send_keys(Keys.RETURN)

# Four different types of call the same content position
print (driver.find_element_by_id("MainContent_Table1"))
print (driver.find_element_by_tag_name("table"))
print (driver.find_element_by_xpath('//div/table[1]'))
print (driver.find_element_by_xpath("//form[@id='Form1']//table[@id='MainContent_Table1']"))

# Call the form that contain all output information 
print (driver.find_element_by_xpath("/html/body/form[1]"))

#print (driver.page_source)

assert "No results found" not in driver.page_source
driver.close()

执行脚本时,输出如下:

  1. 打开firefox浏览器并输入发送的单词并执行"enter"(输入).形式动作

我在打印说明中的输出如下:

/home/bgarcial/.virtualenvs/test/bin/python /home/bgarcial/workspace/Test/example/search.py
<selenium.webdriver.remote.webelement.WebElement (session="7f1ace67-0fb3-42b6-9cc9-af9c58b1715e", element="{320d5570-1060-4d4a-a5e6-af557f28f228}")>
<selenium.webdriver.remote.webelement.WebElement (session="7f1ace67-0fb3-42b6-9cc9-af9c58b1715e", element="{320d5570-1060-4d4a-a5e6-af557f28f228}")>
<selenium.webdriver.remote.webelement.WebElement (session="7f1ace67-0fb3-42b6-9cc9-af9c58b1715e", element="{320d5570-1060-4d4a-a5e6-af557f28f228}")>
<selenium.webdriver.remote.webelement.WebElement (session="7f1ace67-0fb3-42b6-9cc9-af9c58b1715e", element="{320d5570-1060-4d4a-a5e6-af557f28f228}")>
<selenium.webdriver.remote.webelement.WebElement (session="7f1ace67-0fb3-42b6-9cc9-af9c58b1715e", element="{d16859b4-2029-4ba9-8eeb-d3c8371053eb}")>

Process finished with exit code 0

在element属性中返回我要问的元素形式的对象表示形式或内存地址(?-是吗?-)

我该如何检索内容或信息,在这种情况下为竞争"一词.已处理,对于这种情况,分成了竖起杠(Com-pe-ti-tion),而不是我当前收到的字符串或表示形式?

谢谢

解决方案

您看到的是

请注意,找到元素后,我们将获得.text来检索元素文本.


您可能会遇到计时问题-寻找该元素时可能不存在.如果是这种情况,请显式等待进行救援:

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

element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, "table#MainContent_Table1 > tbody > tr > td:nth-of-type(2)"))
)
print(element.text)


演示:

In [1]: from selenium import webdriver

In [2]: from selenium.webdriver.common.keys import Keys

In [3]: 
In [3]: driver = webdriver.Firefox()

In [4]: driver.get("http://tip.iatext.ulpgc.es/silabas/Default.aspx")

In [5]: elem = driver.find_element_by_id("MainContent_TextBox1")

In [6]: elem.send_keys("Competencia")

In [7]: elem.send_keys(Keys.RETURN)

In [8]: print(driver.find_element_by_css_selector("table#MainContent_Table1 > tbody > tr > td:nth-of-type(2)").text)
Com-pe-ten-cia

I have the following situation:

I am using the selenium python binding for send to a web page one word, this web site perform some processing (divide the word in the silabes that contain it) and show to the user the result in the following picture of this way:

Word: Competition

Result: Word "Competition" divide in silabes

In the inspect element tool, I've been checking the id's attributes for the input text

From the following python script through selenium Web driver API, I am sending some word to the search input text and press ENTER Key too for perform the action.

# Currently supported WebDriver implementations are Firefox, Chrome, Ie and Remote
from selenium import webdriver

# Interacting with the RETURN KEY
from selenium.webdriver.common.keys import Keys

# Creating a web driver firefox instance
driver = webdriver.Firefox()

# With the get method we go to the webpage in the url given
driver.get("http://tip.iatext.ulpgc.es/silabas/Default.aspx")

# Assertion that checks if the word "Silabeador" is in the title webpage
assert "Silabeador" in driver.title

# WebDriver let me interact with items in a web page through of their attributtes
# More information http://selenium-python.readthedocs.org/locating-elements.html#locating-elements
elem = driver.find_element_by_id("MainContent_TextBox1")
elem.send_keys("Competencia")
elem.send_keys(Keys.RETURN)

# Four different types of call the same content position
print (driver.find_element_by_id("MainContent_Table1"))
print (driver.find_element_by_tag_name("table"))
print (driver.find_element_by_xpath('//div/table[1]'))
print (driver.find_element_by_xpath("//form[@id='Form1']//table[@id='MainContent_Table1']"))

# Call the form that contain all output information 
print (driver.find_element_by_xpath("/html/body/form[1]"))

#print (driver.page_source)

assert "No results found" not in driver.page_source
driver.close()

And my output, when I execute the script is the following:

  1. Open the firefox browser and put the word sent and execute the "enter" form action

And my output in the print instructions is the following:

/home/bgarcial/.virtualenvs/test/bin/python /home/bgarcial/workspace/Test/example/search.py
<selenium.webdriver.remote.webelement.WebElement (session="7f1ace67-0fb3-42b6-9cc9-af9c58b1715e", element="{320d5570-1060-4d4a-a5e6-af557f28f228}")>
<selenium.webdriver.remote.webelement.WebElement (session="7f1ace67-0fb3-42b6-9cc9-af9c58b1715e", element="{320d5570-1060-4d4a-a5e6-af557f28f228}")>
<selenium.webdriver.remote.webelement.WebElement (session="7f1ace67-0fb3-42b6-9cc9-af9c58b1715e", element="{320d5570-1060-4d4a-a5e6-af557f28f228}")>
<selenium.webdriver.remote.webelement.WebElement (session="7f1ace67-0fb3-42b6-9cc9-af9c58b1715e", element="{320d5570-1060-4d4a-a5e6-af557f28f228}")>
<selenium.webdriver.remote.webelement.WebElement (session="7f1ace67-0fb3-42b6-9cc9-af9c58b1715e", element="{d16859b4-2029-4ba9-8eeb-d3c8371053eb}")>

Process finished with exit code 0

In the element attribute is returning the object representation or memory address (? - Is this right? -) of the elements form that I am asking ...

How to can I retrieve the content or information, in this case the word "Competition" processed, for this situation divided in silabes (Com-pe-ti-tion)and not the string or representations that I currently receive?

Thanks

解决方案

What you see printed are the WebElement instance string representations.

You need to locate the second row in the table and get the second cell. You can do it in one go:

driver.find_element_by_css_selector("table#MainContent_Table1 > tbody > tr > td:nth-of-type(2)").text

Note that, once we've located the element, we are getting the .text to retrieve the element text.


You may encounter timing issues - the element might not be present when you look for it. If this is a case, Explicit Wait is to the rescue:

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

element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, "table#MainContent_Table1 > tbody > tr > td:nth-of-type(2)"))
)
print(element.text)


Demo:

In [1]: from selenium import webdriver

In [2]: from selenium.webdriver.common.keys import Keys

In [3]: 
In [3]: driver = webdriver.Firefox()

In [4]: driver.get("http://tip.iatext.ulpgc.es/silabas/Default.aspx")

In [5]: elem = driver.find_element_by_id("MainContent_TextBox1")

In [6]: elem.send_keys("Competencia")

In [7]: elem.send_keys(Keys.RETURN)

In [8]: print(driver.find_element_by_css_selector("table#MainContent_Table1 > tbody > tr > td:nth-of-type(2)").text)
Com-pe-ten-cia

这篇关于从网页发送和接收数据-Selenium的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆