简体   繁体   English

Python Selenium:查找并单击一个元素

[英]Python Selenium: find and click an element

How do I use Python to simply find a link containing text/id/etc and then click that element? 如何使用Python简单地找到包含text / id / etc的链接然后单击该元素?

My imports: 我的进口:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys

Code to go to my URL: 要转到我的网址的代码:

browser = webdriver.Firefox() # Get local session of firefox
browser.get(myURL) # Load page
#right here I want to look for element containing "Click Me" in the text 
#and then click it

Look at method list of WebDriver class - http://selenium.googlecode.com/svn/trunk/docs/api/py/webdriver_remote/selenium.webdriver.remote.webdriver.html 查看WebDriver类的方法列表 - http://selenium.googlecode.com/svn/trunk/docs/api/py/webdriver_remote/selenium.webdriver.remote.webdriver.html

For example, to find element by its ID and click it, the code will look like this 例如,要按ID查找元素并单击它,代码将如下所示

element = browser.find_element_by_id("your_element_id")
element.click()

for link containing text 包含文本的链接

browser = webdriver.firefox()
browser.find_element_by_link_text(link_text).click()

You just use this code 你只需使用这段代码

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

driver = webdriver.Firefox()
driver.get("https://play.spotify.com/")# here change your link

wait=WebDriverWait(driver,250)
# it will wait for 250 seconds an element to come into view, you can change the #value

submit=wait.until(EC.presence_of_element_located((By.LINK_TEXT, 'Click Me')))
submit.click()

here wait is best fit for javascript enabled website let suppose if you load the page in webdriver but due to javascript some element loads after some second then it best suite to use wait element there. 这里等待是最适合启用javascript的网站让我们假设如果你在webdriver中加载页面但由于javascript某些元素在一些秒后加载然后它最好套件在那里使用wait元素。

Hi there are a couple of ways to find a link (or any element): 您好,有几种方法可以找到链接(或任何元素):

element = driver.find_element_by_id("element-id")
element = driver.find_element_by_name("element-name")
element = driver.find_element_by_xpath("//input[@id='element-id']")
element = driver.find_element_by_link_text("link-text")
element = driver.find_element_by_class_name("class-name")

I think the best option for you is find_element_by_link_text since it's a link. 我认为最好的选择是find_element_by_link_text因为它是一个链接。 Once you saved the element in a variable, you call the click function: element.click() or element.send_keys(Keys.RETURN) 将元素保存在变量中后,可以调用click函数: element.click()element.send_keys(Keys.RETURN)

Take a look to the selenium-python documentation , there are a couple of examples there. 看看selenium-python文档 ,那里有几个例子。

Try SST - it is a simple yet very good test framework for python. 尝试SST - 它是一个简单但非常好的python测试框架。 Install it first: http://testutils.org/sst/index.html 首先安装它: http//testutils.org/sst/index.html

Then: 然后:

Imports: 进口:

from sst.actions import * 

First define a variable - element - that you're after 首先定义一个你想要的变量element

element = assert_element(text="some words and more words")

I used this: http://testutils.org/sst/actions.html#assert-element 我用过这个: http//testutils.org/sst/actions.html#assert-element

Then click that element: 然后单击该元素:

click_element('element')

And that's it. 就是这样。

您也可以通过xpath执行此操作:

Browser.find_element_by_xpath("//a[@href='you_link']").click()

Finding Element by Link Text 通过链接文本查找元素

driver.find_element_by_link_text("")

Finding Element by Name 按名称查找元素

driver.find_element_by_name("")

Finding Element By ID 按ID查找元素

driver.find_element_by_id("")

First start one instance of browser. 首先启动一个浏览器实例。 Then you can use any of the following methods to get element or link. 然后,您可以使用以下任何方法来获取元素或链接。 After locating the element you can use element.click() or element.send_keys(Keys.RETURN) or any other object of selenium webdriver 找到元素后,您可以使用element.click()element.send_keys(Keys.RETURN)selenium webdriver任何其他对象

browser = webdriver.Firefox()

Selenium provides the following methods to locate elements in a page: Selenium提供了以下方法来定位页面中的元素:

To find individual elements. 找到个别元素。 These methods will return individual element. 这些方法将返回单个元素。

browser.find_element_by_id(id)
browser.find_element_by_name(name)
browser.find_element_by_xpath(xpath)
browser.find_element_by_link_text(link_text)
browser.find_element_by_partial_link_text(partial_link_text)
browser.find_element_by_tag_name(tag_name)
browser.find_element_by_class_name(class_name)
browser.find_element_by_css_selector(css_selector)

To find multiple elements (these methods will return a list). 要查找多个元素(这些方法将返回一个列表)。 Later you can iterate through the list or with elementlist[n] you can locate individual element from list. 稍后您可以遍历列表或使用elementlist[n]您可以从列表中找到单个元素。

browser.find_elements_by_name(name)
browser.find_elements_by_xpath(xpath)
browser.find_elements_by_link_text(link_text)
browser.find_elements_by_partial_link_text(partial_link_text)
browser.find_elements_by_tag_name(tag_name)
browser.find_elements_by_class_name(class_name)
browser.find_elements_by_css_selector(css_selector)
browser.find_element_by_xpath("")
browser.find_element_by_id("")
browser.find_element_by_name("")
browser.find_element_by_class_name("")

Inside the ("") you have to put the respective xpath, id, name, class_name, etc... 在(“”)内你必须放置相应的xpath,id,name,class_name等...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM