简体   繁体   English

如何在 selenium web 驱动程序中访问没有名称属性的 div 按钮?

[英]How to access button of div that has no name attribute in selenium web driver?

I have written this code and want to log in to Twitter with a selenium web driver .我写了这段代码,想用selenium web driver登录Twitter But after filling in username I am unable to click the next button as it does not have name or id attributes.但是在填写用户名后,我无法单击下一步按钮,因为它没有nameid属性。 How can I do it?我该怎么做?

from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://twitter.com/login?lang=en-gb")
time.sleep(3)
driver.find_element_by_name('text').send_keys('username')
time.sleep(3)
driver.find_element_by_class_name('').click()

time.sleep(3)
print('TEst Completed')

To click on the element Next you can use either of the following Locator Strategies :要单击元素Next ,您可以使用以下任一Locator Strategies

  • Using xpath :使用xpath

     driver.find_element(By.XPATH, "//span[text()='Next']").click()

In Chrome you can bring up the inspector by right-mouse clicking on the element of interest (the Next button) and selecting Insepct from the popup).在 Chrome 中,您可以通过右键单击感兴趣的元素( Next按钮)并从弹出窗口中选择 Insepct 来调出检查器。 Then in the Inspector window, right-mouse click on the element you are interested in (it should already be highlighted), and select Copy and then Copy full XPath .然后在 Inspector window 中,右键单击您感兴趣的元素(它应该已经突出显示),然后 select Copy然后Copy full XPath You can then paste the result in to a find_element_by_xpath call.然后,您可以将结果粘贴到find_element_by_xpath调用中。

As an aside, using time.sleep calls are fine if you want your program to run in slow motion so you can see what is happening, but if you want it to run at top speed, you should initially call driver.implcitly_wait(n) where n is some number of seconds (for example, 3).顺便说一句,如果您希望程序以慢动作运行,那么使用time.sleep调用很好,这样您就可以看到正在发生的事情,但是如果您希望它以最高速度运行,您应该首先调用driver.implcitly_wait(n)其中n是秒数(例如,3)。 Then when you do a find_element call following driver.get , the driver will wait for up to 3 seconds for the desired element to appear before timing out but will not wait any longer than necessary.然后,当您在 driver.get 之后执行driver.get调用时,驱动程序将等待最多 3 秒,以便在超时之前出现所需的元素,但不会等待超过必要的时间。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())

try:
    driver = webdriver.Chrome()
    driver.maximize_window()
    # Wait for up to 3 seconds (but maybe less) for sought after
    # elemt to appear:
    driver.implicitly_wait(3)
    driver.get("https://twitter.com/login?lang=en-gb")
    driver.find_element_by_name('text').send_keys('username')
    driver.find_element_by_xpath('/html/body/div/div/div/div[1]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div[6]/div/span/span').click()
    print('Test Completed')
except Exception as e:
    print("Got exception:", e)
finally:
    # Come here even on exceptions to be sure we "quit" the driver:
    input('Pausing until you hit enter...')
    driver.quit()

No every element will have some fixed id or name or even class attribute.不是每个元素都有一些固定的idname ,甚至class属性。
In this particular case elements are having dynamic class names, so the most stable approach here will to use the Next text.在这种特殊情况下,元素具有动态 class 名称,因此此处最稳定的方法将使用Next文本。
This can be done with XPath locator, as following:这可以通过 XPath 定位器来完成,如下所示:

driver.find_element_by_xpath("//span[text()='Next']").click()

暂无
暂无

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

相关问题 Selenium Web 驱动程序:如何单击没有 ID、NAME 的图像按钮 - Selenium Web driver: how do I click on image button without ID, NAME Python-Selenium Web驱动程序错误-self._driver.execute-AttributeError:“ unicode”对象没有属性“ id” - Python - Selenium Web Driver error - self._driver.execute - AttributeError: 'unicode' object has no attribute 'id' pycharm- python 与 selenium web 驱动程序。 得到错误 object has no attribute driver - pycharm- python with selenium web driver. Get error object has no attribute driver 如何处理在 python 中的 selenium web 驱动程序中单击“下载键”按钮? - How to handle click on “Download key” button in selenium web driver in python? Selenium Web驱动程序:如何单击按钮? - Selenium web driver: How do I click on the button? Selenium Python 错误'对象没有属性驱动程序' - Selenium Python error 'object has no attribute driver' Selenium 'FirefoxWebElement' object 没有属性 '_driver' - Selenium 'FirefoxWebElement' object has no attribute '_driver' Selenium web 驱动找不到按钮 - Selenium web driver cannot find button 如何访问硒中具有相同类名的第二个元素 - How to access the second element that has the same class name in selenium Web 抓取 Selenium 错误:“with AttributeError: 'list' object 没有属性 'find_element_by_class_name'” - Web scraping Selenium Error: "with AttributeError: 'list' object has no attribute 'find_element_by_class_name' "
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM