[英]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.但是在填写用户名后,我无法单击下一步按钮,因为它没有
name
或id
属性。 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.不是每个元素都有一些固定的
id
或name
,甚至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.