我應(yīng)該如何編寫 el3
才能點(diǎn)擊 關(guān)閉 按鈕?
解決方案
Leon's注釋 導(dǎo)致不再支持復(fù)合類名稱的正確信息.你可以做的是嘗試使用 css 選擇器.在您的情況下,以下代碼行應(yīng)該可以幫助您獲得所需的元素:
el3 = driver.find_element_by_css_selector(".action-btn.cancel.alert-display")
它在 class 屬性中找到具有所有三個(gè)類(action-btn、cancel 和 alert-display)的元素.請(qǐng)注意,類的順序在這里無關(guān)緊要,任何類都可能出現(xiàn)在類屬性中的任何位置.只要元素具有所有三個(gè)類,它就會(huì)被選中.如果要固定類的順序,可以使用以下 xpath :
el3 = driver.find_element_by_xpath("http://*[@class='action-btn cancel alert-display']")
I have the below code that clicks on an element to pop up a screen and copy the text in it
el1 = driver.find_element_by_id("keyDev-A")
el1.click()
el2 = driver.find_element_by_class_name("content")
print(el2.text)
However, when I tried to get selenium
to click on the button within that popup with
el3 = driver.find_element(By.CLASS_NAME, "action-btn cancel alert-display")
el3.click()
It produces an error message:
invalid selector: Compound class names not permitted
This is the HTML that I am trying to get selenium
to click on. The Close
button.
<div class="nav">
<span class="action-btn confirm prompt-display">Confirm</span>
<span class="action-btn cancel prompt-display">Cancel</span>
<span class="action-btn cancel alert-display">Close</span>
</div>
How should I be writing el3
in order to click on the Close button?
解決方案
Leon's comment leads to the correct information that compound class names are no longer supported. What you could do instead is try using css selectors. In your case, the following line of code should help you get the element you want :
el3 = driver.find_element_by_css_selector(".action-btn.cancel.alert-display")
It finds the element with all three classes (action-btn, cancel and alert-display) in the class attribute. Do note that the order of the classes does not matter here and any of the classes may appear anywhere in the class attribute. As long as the element has all three classes, it will be selected.
If you want the order of the classes to be fixed, you can use the following xpath :
el3 = driver.find_element_by_xpath("http://*[@class='action-btn cancel alert-display']")
這篇關(guān)于不允許使用硒化合物類名稱的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!