Javascript 单击复选框在第一次起作用,但在第二次不起作用[Selenium]
所以我需要开发票 首先,我单击复选框以选择当前页面上的所有发票。然后我点击发布按钮,最后批准发布 当前发票发出后,下一页上的发票将自动显示 我想为下一个重复相同的过程,但我不能像第一次那样单击复选框。没有发生任何事情,它不会给出任何错误。只是什么都没发生 这是html @国王11Javascript 单击复选框在第一次起作用,但在第二次不起作用[Selenium],javascript,python,html,selenium,automation,Javascript,Python,Html,Selenium,Automation,所以我需要开发票 首先,我单击复选框以选择当前页面上的所有发票。然后我点击发布按钮,最后批准发布 当前发票发出后,下一页上的发票将自动显示 我想为下一个重复相同的过程,但我不能像第一次那样单击复选框。没有发生任何事情,它不会给出任何错误。只是什么都没发生 这是html @国王11 time.sleep(8) --wait until invoice screen appears driver.find_element_by_css_selector("i[class='ion-android-c
time.sleep(8) --wait until invoice screen appears
driver.find_element_by_css_selector("i[class='ion-android-checkbox-outline-blank']").click() --selecting checkbox
driver.find_element_by_xpath("//button[contains(text(),'Sil')]").click() --clicking on the sent button
driver.find_element_by_xpath("//button[contains(text(),'Evet')]").click() --approve to sent
time.sleep(12) --wait till current invoices are sent and checkbox become re-eligible to select
driver.find_element_by_css_selector("i[class='ion-android-checkbox-outline-blank']").click() --select the check box second time. used javascript to eleminate GUI issues but it does not do clicking
编辑:我通过编写一个循环找到了解决方案,每次它发出发票时都会刷新浏览器
def loopy(page_number):
while (page_number>0):
driver.refresh()
time.sleep(5)
element_1=driver.find_element_by_xpath("/html/body/app/main/app/div/div/e-arsiv/taslaklar/div[2]/div/div/div[3]/div/button")
driver.execute_script("arguments[0].click();", element_1)
element_2=driver.find_element_by_xpath("/html/body/app/main/app/div/div/e-arsiv/taslaklar/div[2]/div/div/div[3]/div/ul/button[7]")
driver.execute_script("arguments[0].click();", element_2)
driver.implicitly_wait(20)
driver.find_element_by_css_selector ("uc-quick-filter[titleshort='3A'][type='3M']").click()
driver.implicitly_wait(20)
element_4=driver.find_element_by_xpath("/html/body/app/main/app/div/div/e-arsiv/taslaklar/div[2]/div/div/div[4]/div[2]/ul/button[text()='e-Arşiv']")
driver.execute_script("arguments[0].click();", element_4)
wait = WebDriverWait(driver, 40)
temp = wait.until(EC.element_to_be_clickable((By.XPATH,"//e-arsiv/taslaklar/div[4]/div[3]/div/div/i")))
action= ActionChains(driver)
action.click(temp).perform()
element_5=driver.find_element_by_xpath("//button[contains(text(),'Sil')]")
driver.execute_script("arguments[0].click();", element_5)
element_6=driver.find_element_by_xpath("//button[contains(text(),'Evet')]")
driver.execute_script("arguments[0].click();", element_6)
driver.implicitly_wait(20)
service=driver.find_element_by_id('serviceLoading').get_attribute("style")
while (service=='display: none;'):
driver.refresh()
page_number=page_number-1
time.sleep(5)
break
else:
time.sleep(2)
service=driver.find_element_by_id('serviceLoading').get_attribute("style")
尝试使用
ActionsChains
。首先,您必须使用以下行导入它:
来自selenium.webdriver.common.action\u chains导入ActionChains
time.sleep(8) --wait until invoice screen appears
WebElement temp = driver.findElement(By.cssSelector("i[class='ion-android-checkbox-outline-blank']"));
action=ActionChains(driver)
action.click(temp).preform(); --selecting checkbox
driver.find_element_by_xpath("//button[contains(text(),'Sil')]").click() --clicking on the sent button
driver.find_element_by_xpath("//button[contains(text(),'Evet')]").click() --approve to sent
time.sleep(12) --wait till current invoices are sent and checkbox become re-eligible to select
action.click(temp).preform();
希望这会有所帮助。请等待复选框元素变为可单击,然后执行单击
element = WebDriverWait(driver, 10).until(
driver.element_to_be_clickable((By.xpath, "//button[contains(text(),'Sil')]"));
element.click
)
没有其他原因会导致失败 您是否尝试过调试并逐步完成每个步骤?最后一行是不起作用的,但我不知道如何调试,因为它是单行代码,应该可以工作。驱动程序。通过css选择器(“i[class='ion-android-checkbox-outline-blank'])查找元素。单击()-``创建一个变量,查看该变量是否是您要单击的项目。例如
var temp=driver.find_element_by_css_选择器(“i[class='ion-android-checkbox-outline-blank'])
。查看这是否是您想要的元素,然后调用temp.click()代码>以测试它是否执行了需要执行的操作。只要试着把它分成更小的部分,看看哪个部分对你来说工作不正常。它应该是那个变量,因为它在第二行中工作正常,点击复选框。你不这么认为吗?既然这两行是一样的,那么像我之前的评论那样,只需要对这一行做一个变量,然后在这两行调用temp.click()
,不是更容易吗?除了css_选择器之外,你还试过用其他方法获取元素吗?对不起,我是个新手,但是它会给temp带来语法错误。将屏幕截图添加到问题中。错误到底是什么?无需道歉:)我更改了驱动程序.find.Element…
行。尝试使用我刚刚编辑过的更改运行,但现在得到这个:“第52行,在temp=driver.findElement(By.cssSelector(“I[class='ion-android-checkbox-outline-blank']”)AttributeError:'WebDriver'对象没有属性'findElement',通过temp=driver.find_元素(By.XPATH,//e-arsiv/taslaklar/div[4]/div[3]解决了这个问题。”/div/div/i”)
但是,现在获取名称错误:未定义名称“Actions”。我应该导入特定类吗?