Python Selemium无法单击按钮

2024-09-25 18:21:11 发布

您现在位置:Python中文网/ 问答频道 /正文

希望你没事, 请希望你能在这个话题上支持我。 我尝试了很多方法来点击Selenium的这个按钮,但是我做不到。错误是Selenium找不到该对象

ERROR MESSAAGE: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath", "selector":"//*[@id='C47_W166_V167_thtmlb_button_2']"}

下面是要单击的python代码:

window_before = driver.window_handles[0] #Save Current window 
window_after = driver.window_handles[1] #Identify new raised window  
driver.switch_to.window(window_after) #To go to new raised window 

time.sleep(3)

driver.maximize_window()

driver.switch_to.frame("WorkAreaFrame1popup")
print(driver.title)
time.sleep(3)

ALL_BRANDS_BTN= driver.find_element_by_xpath("//*[@id='C51_W184_V185_thtmlb_button_2']")
ALL_BRANDS_BTN.click()

我对同一web工具上的其他按钮也做了同样的操作,没有任何问题,但是对于这个特殊的按钮,我不能。也许我错过了什么? 我已经花了额外的等待时间,但都没用

我附上一个元素检查的图像。求你了,希望你能帮我, 提前谢谢

<a href="javascript:void(0)" class="th-bt th-bt-text" onclick="thBtMgr.click(this);return htmlbSL(this,2,'C51_W184_V185_thtmlb_button_2:EXCEL','0')" onmousedown="thBtMgr.press(this,event);" onfocusout="thBtMgr.unpress(this);" onfocus="thSaveKbFocus(this);" oncontextmenu="return false;" ondragstart="return false;" id="C51_W184_V185_thtmlb_button_2"><span class="th-bt-span"><b class="th-bt-b">All Brands</b></span></a> <span class="th-bt-span"><b class="th-bt-b">All Brands</b></span>

Tags: toiddriverbuttonelementwindowthis按钮
1条回答
网友
1楼 · 发布于 2024-09-25 18:21:11

正如您提到的,您对其他按钮使用了相同的代码,所以我假设您在正确的窗口和iFrame中。如果没有,请确保您的按钮在正确的windoe和框架

您可以尝试以下代码来单击您的按钮:

btn = WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.XPATH, "//b[text()='All Brands']//ancestor::a"))) #Used Text instea of Id , as not sure if ID is dynamic or not
driver.execute_script("arguments[0].scrollIntoView();", btn ) #Scroll to button
driver.execute_script("arguments[0].click();", btn ) #Click then button

相关问题 更多 >