即使该元素在网页中,也无法在xpath中找到该元素。实际上,代码既不会抛出Exception也不会找到元素。
for c in range(sheet.ncols):
for r in range(sheet.nrows):
st = (sheet.cell_value(r, c))
print(str(st))
xpath1 = "//input[@value='Analyze' and contains(@onclick,'" + str(st) + "')]"
#xpath = "//input[@value='Analyze'][.='" + st + "']"
print(driver.title)
print(len(driver.find_elements_by_xpath(xpath1)))
if driver.find_elements_by_xpath(xpath1):
print("loop")
driver.find_element_by_xpath(xpath1).click() # Here new window will open
time.sleep(2)
#Main_Window = driver.current_window_handle
driver.switch_to.window(driver.window_handles[-1])
driver.find_element_by_xpath('/html/body/table/tbody/tr[4]/td/table/tbody/tr[9]/td[3]/input').click()
driver.close()
driver.switch_to.window(driver.window_handles[-1])
xpath2 = "//*[@id='create_button']"
xpath3 = "//*[@id='update_button']"
if check_exists_by_xpath(xpath2):
driver.find_element_by_xpath(xpath2).click()
driver.close()
driver.switch_to.window(driver.window_handles[0])
elif check_exists_by_xpath(xpath3):
driver.close()
driver.switch_to.window(driver.window_handles[0])
continue
预期输出应为:
1635645 ST 1个 环 2M274203FZ ST 1个 2M27430052 ST 1个 1618745 ST 1
但是运行上面的代码时得到以下输出: 1635645 ST 1个 环 2M274203FZ ST 0 2M27430052 ST 0 1618745 ST 0
代码有什么问题?
谢谢。
答案 0 :(得分:1)
只需切换到正确的窗口和框架。
if check_exists_by_xpath(xpath2):
driver.find_element_by_xpath(xpath2).click()
#else not required as you are not using the xpath3 to click
driver.close()
driver.switch_to.window(driver.window_handles[0])
driver.switch_to.frame(base_frame_locator/index)
driver.switch_to.frame(child_frame_locator/index)
continue
答案 1 :(得分:0)
我也想查看您的HTML或获取指向该网页的链接以检查xpath。但是根据您的输出,我认为您可能未切换到[0]独立窗口。您正在if语句中执行窗口切换,因此,如果这两个条件都不满足,则不会切换窗口。尝试从窗口开关中取出窗口开关,
for c in range(sheet.ncols):
for r in range(sheet.nrows):
st = (sheet.cell_value(r, c))
print(str(st))
xpath1 = "//input[@value='Analyze' and contains(@onclick,'" + str(st) + "')]"
#xpath = "//input[@value='Analyze'][.='" + st + "']"
print(driver.title)
print(len(driver.find_elements_by_xpath(xpath1)))
if driver.find_elements_by_xpath(xpath1):
print("loop")
driver.find_element_by_xpath(xpath1).click() # Here new window will open
time.sleep(2)
#Main_Window = driver.current_window_handle
driver.switch_to.window(driver.window_handles[-1])
driver.find_element_by_xpath('/html/body/table/tbody/tr[4]/td/table/tbody/tr[9]/td[3]/input').click()
driver.close()
driver.switch_to.window(driver.window_handles[-1])
xpath2 = "//*[@id='create_button']"
xpath3 = "//*[@id='update_button']"
if check_exists_by_xpath(xpath2):
driver.find_element_by_xpath(xpath2).click()
driver.close()
driver.switch_to.window(driver.window_handles[0])
我认为您甚至不需要第二个if语句,因为它只包含
driver.close()
driver.switch_to.window(driver.window_handles[0])