简体   繁体   English

Python-Selenium: Select object 由 xpath 工作〜80%的时间,失败其他

[英]Python-Selenium: Select object by xpath works ~80% of the time, fails others

I have a program with several find_element/click statements and most work, but this is the only problem statement:我有一个程序有几个 find_element/click 语句和大多数工作,但这是唯一的问题语句:

wait.until(lambda driver: browser.find_element_by_xpath('/html/body/table[3]/tbody/tr/td/table/tbody/tr/td[1]/table/tbody/tr/td[1]/table[2]/tbody/tr[2]/td'))
print('Accessing Virtual Warehouse')
VirtWhse = browser.find_element_by_xpath('/html/body/table[3]/tbody/tr/td/table/tbody/tr/td[1]/table/tbody/tr/td[1]/table[2]/tbody/tr[2]/td')
VirtWhse.click()

the wait function was added because I thought maybe it was trying to click before the element was loaded.添加了等待 function 是因为我认为它可能在加载元素之前尝试单击。 it doesn't seem to make any difference if it is added or not.添加与否似乎没有任何区别。 it is defined as:它被定义为:

browser = webdriver.Chrome
wait = ui.WebDriverWait(browser,10)

I know that it is finding the element because my print statement is always outputted.我知道它正在寻找元素,因为我的打印语句总是被输出。 also, I can see the link being underlined in the browser when I watch it being executed.另外,当我看到它被执行时,我可以在浏览器中看到该链接带有下划线。

the problem is, it only seems to actually click on the link 80% of the time.问题是,它似乎只有 80% 的时间实际上点击了链接。 The rest of the time, my wait.until function times out and the program crashes.当时的rest,我的wait.until function超时,程序崩溃。 (EDIT- the wait function that times out is the next wait function, after the click, not the one in the code snippet.) (编辑 - 等待 function 超时是单击后的下一个等待 function,而不是代码片段中的那个。)

Can anyone explain why I'm having this reliability issue with this one element?谁能解释为什么我对这个元素有这个可靠性问题?

I added time.sleep(.5) before VirtWhse.click() per @JackWu and it seems to solve the issue.我在每个@JackWu 的 VirtWhse.click() 之前添加了 time.sleep(.5) ,它似乎解决了这个问题。

I thought my initial wait.until function would prevent this from being needed, but I guess it just needs a little time between defining this particular object and selecting it for some reason.我以为我最初的 wait.until function 会阻止它被需要,但我想在定义这个特定的 object 和出于某种原因选择它之间只需要一点时间。

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

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