Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 无法单击元素_Python 3.x_Selenium_Selenium Webdriver - Fatal编程技术网

Python 3.x 无法单击元素

Python 3.x 无法单击元素,python-3.x,selenium,selenium-webdriver,Python 3.x,Selenium,Selenium Webdriver,'我无法单击该元素。代码不允许我看到异常跳转到Exception块。在除此之外的块中,流量控制后的前2行也将用于范围内的c(sheet.ncols): 对于范围内的r(sheet.nrows):此处无法找到或单击xpath。代码输出是 进程已完成,退出代码为0 from selenium import webdriver import time from selenium.common.exceptions import NoSuchElementException import xlrd d

'我无法单击该元素。代码不允许我看到异常跳转到Exception块。在除此之外的块中,流量控制后的前2行也将用于范围内的c(sheet.ncols): 对于范围内的r(sheet.nrows):此处无法找到或单击xpath。代码输出是 进程已完成,退出代码为0

from selenium import webdriver
import time

from selenium.common.exceptions import NoSuchElementException
import xlrd
driver = webdriver.Chrome(r'chromedriver.exe')
driver.maximize_window()
driver.get('url')
driver.switch_to.frame('m')
emailElem = driver.find_element_by_xpath('//*[@id="log_main"]/input')

# emailElem=browser.find_element_by_id('l')
# emailElem=browser.find_element_by_name("id")
emailElem.send_keys('sf@df.com')
# time.sleep(5)
passwordElem = driver.find_element_by_xpath('//*[@id="log_main"]/p[1]/input')
passwordElem.send_keys('#####')

passwd=passwordElem.find_element_by_xpath('//*[@id="log_main"]/p[2]/input').click()
driver.switch_to.parent_frame()
driver.switch_to.frame('topFrame')

driver.find_element_by_css_selector('.menu > ul:nth-child(1) > li:nth-child(7) > a:nth-child(1) > span:nth-child(1)').click()#Reports
driver.switch_to.parent_frame()
driver.switch_to.frame('ST_mainFrame')
driver.find_element_by_css_selector('div.menu_row:nth-child(16) > span:nth-child(3) > label:nth-child(2)').click() #FTO


driver.switch_to.frame('fto_details')
time.sleep(8)
driver.find_element_by_css_selector('html body table tbody tr td form table tbody tr td a img').click()#search





path = r'C:\Users\username\PycharmProjects\Python\WebScraping\Book2.xlsx'
workbook = xlrd.open_workbook(path)
sheet = workbook.sheet_by_index(0)


"""def check_exists_by_xpath(xpath):
    try:
        driver.find_element_by_xpath(xpath)
    except NoSuchElementException:
        return False

    return True
    """
for c in range(sheet.ncols):
    for r in range(sheet.nrows):
        st = (sheet.cell_value(r, c))
        try:
            if st == float(st):
                st_string = str(int(st))



                xpath = "//input[@value='Analyze' and contains(@onclick,'" + st_string + "')]"
                #cont = 0
                if driver.find_elements_by_xpath(xpath):
                    driver.find_element_by_xpath(xpath).click() #Here new window will open
                    handles = driver.window_handles
                    h = len(handles)#h=2

                    #print(handles)
                    for handle in range(1,h):
                    #for handle in range(h):

                        driver.switch_to.window(handles[handle])
                        #driver.find_element_by_xpath('/html/body/table/tbody/tr[4]/td/table/tbody/tr[9]/td[3]/input').click()
                        driver.find_element_by_css_selector('.t2').click()
                        #time.sleep(2)
                        #driver.close()

                        lst_window = driver.window_handles
                        h2 = len(lst_window) #h2=2
                        def check_exists_by_xpath(xpath):
                            try:
                                driver.find_element_by_xpath(xpath)
                            except NoSuchElementException:
                                return False
                            return True

                        for lst_handle in range(2,h2):
                        #for lst_handle in range(1,h2):
                            driver.switch_to.window(lst_window[lst_handle])
                            xpath2 = "//*[@id='create_button']"
                            xpath3="//*[@id='update_button']"
                            #css2 = '#create_button'
                            #css3 = '#update_button'
                            if check_exists_by_xpath(xpath2):
                                driver.find_element_by_xpath(xpath2).click()
                                #driver.close()

                            elif check_exists_by_xpath(xpath3):
                                driver.find_element_by_xpath(xpath3).click()
                                #driver.close()

                           #if driver.find_element_by_css_selector(css2):
                            #    driver.find_element_by_css_selector(css2).click()
                            #elif driver.find_element_by_css_selector(css3):
                             #   driver.find_element_by_css_selector(css3).click()

                            #if driver.find_element_by_xpath(xpath2) or driver.find_element_by_xpath(xpath3):
                             #   driver.find_element_by_xpath(xpath2 or xpath3).click()
                              #  driver.close()
                            #elif driver.find_element_by_xpath(xpath3):
                                #driver.find_element_by_xpath(xpath3).click()
                                #driver.close()





                #cont+=1
            #print(cont)



        #except ValueError:
        except Exception:
            if st == str(st):
                xpath = "//input[@value='Analyze' and contains(@onclick,'" + st + "')]"
                #cont1=0
                if driver.find_elements_by_xpath(xpath):
                    driver.find_element_by_xpath(xpath).click()  # Here new window will open
                    handles = driver.window_handles
                    h = len(handles)  # h=2

                    # print(handles)
                    for handle in range(1, h):
                        # for handle in range(h):

                        driver.switch_to.window(handles[handle])
                        driver.find_element_by_xpath('/html/body/table/tbody/tr[4]/td/table/tbody/tr[9]/td[3]/input').click()
                        #time.sleep(5)
                        #driver.close()

                        lst_window = driver.window_handles
                        h2 = len(lst_window)  # h2=2
                        def check_exists_by_xpath(xpath):
                            try:
                                driver.find_element_by_xpath(xpath)
                            except NoSuchElementException:
                                return False
                            return True
                        #for lst_handle in range(1, h2):
                        for lst_handle in range(2,h2):
                            driver.switch_to.window(lst_window[lst_handle])
                            xpath2 = "//*[@id='create_button']"
                            xpath3 = "//*[@id='update_button']"
                            if check_exists_by_xpath(xpath2):
                                driver.find_element_by_xpath(xpath2).click()
                                #driver.close()

                            elif check_exists_by_xpath(xpath3):
                                driver.find_element_by_xpath(xpath3).click()
                                #driver.close()


"""
                            if driver.find_element_by_xpath(xpath2):
                                driver.find_element_by_xpath(xpath2).click()
                                driver.close()
                            elif driver.find_element_by_xpath(xpath3):
                                driver.find_element_by_xpath(xpath3).click()
                                """
我的代码有什么问题。 在excel中,我有n个值(行),其中只有第一个值代码成功执行。其余的价值正在上升,但

xpath = "//input[@value='Analyze' and contains(@onclick,'" + st_string + "')]"
                #cont = 0
                if driver.find_elements_by_xpath(xpath):
在这段代码之后,控件将返回Excel(for循环)并在一段时间内抛出ValueError

except Exception:
    if st == str(st):
        xpath = "//input[@value='Analyze' and contains(@onclick,'" + st + "')]"
        #cont1=0
这里抛出一个异常作为

Traceback (most recent call last): File "m.py", line 53, in <module> if st == float(st): ValueError: could not convert string to float: '23' During handling of the above exception, another exception occurred: Traceback (most recent call last):
Traceback(最近一次调用):文件“m.py”,第53行,if st==float(st):ValueError:无法将字符串转换为float:'23'在处理上述异常期间,发生了另一个异常:Traceback(最近一次调用):

有人能帮我吗,谢谢。

根据您的最新评论,第二个窗口(至少)似乎不会包含
“/*[@id='create\u button']]”或
中的至少一个。
“/*[@id='update\u button']”。在此情况下,在下面的代码块中

if driver.find_element_by_xpath(xpath2):
    driver.find_element_by_xpath(xpath2).click()
    driver.close()
elif driver.find_element_by_xpath(xpath3):
    driver.find_element_by_xpath(xpath3).click()
    driver.close()
至少有一个

if driver.find_element_by_xpath(xpath2):

将抛出一个
ElementNotFoundException
将您发送到
except

这是因为执行
driver.find\u element\u by\u xpath()
会在元素未定位时抛出此类错误

我不确定你想完成什么,或者你的页面看起来是什么样子,所以我不知道要做什么修复,但这似乎是你问题的原因。如果你能提供更多的细节,确切地说,你正试图做这个循环,我可能能够建议一个修复

再想一想,以下方法可能有效:

在某处添加此导入和功能:

from selenium.common.exceptions import NoSuchElementException        
def check_exists_by_xpath(xpath):
try:
    webdriver.find_element_by_xpath(xpath)
except NoSuchElementException:
    return False
return True
然后,将原始if块更改为:

if check_exists_by_xpath(xpath2):
    driver.find_element_by_xpath(xpath2).click()
    driver.close()
elif check_exists_by_xpath(xpath3):
    driver.find_element_by_xpath(xpath3).click()
    driver.close()

编辑这和我想的差不多。有帮助吗

from selenium import webdriver
import time
from selenium.common.exceptions import NoSuchElementException
import xlrd

driver = webdriver.Chrome(r'C:\Users\tmou\PycharmProjects\Python\WebScraping\chromedriver.exe')
driver.maximize_window()
driver.get('https://www.stew.cyz.com/st/st_main.php')
driver.switch_to.frame('ST_mainFrame')
emailElem = driver.find_element_by_xpath('//*[@id="log_main"]/input')

# emailElem=browser.find_element_by_id('log_main')
# emailElem=browser.find_element_by_name("hpuser_id")
emailElem.send_keys('xyn@hyo.com')
# time.sleep(5)
passwordElem = driver.find_element_by_xpath('//*[@id="log_main"]/p[1]/input')
passwordElem.send_keys('********')
passwd = passwordElem.find_element_by_xpath('//*[@id="log_main"]/p[2]/input').click()
driver.switch_to.parent_frame()
driver.switch_to.frame('topFrame')

driver.find_element_by_css_selector(
'.menu > ul:nth-child(1) > li:nth-child(7) > a:nth-child(1) > span:nth-child(1)').click()  # Reports
driver.switch_to.parent_frame()
driver.switch_to.frame('ST_mainFrame')
driver.find_element_by_css_selector(
'div.menu_row:nth-child(16) > span:nth-child(3) > label:nth-child(2)').click()  # FTO

driver.switch_to.frame('fto_details')
time.sleep(8)
driver.find_element_by_css_selector('html body table tbody tr td form table tbody tr td a img').click()  # search


path = r'C:\Users\tmou\PycharmProjects\Python\WebScraping\Book2.xlsx'
workbook = xlrd.open_workbook(path)
sheet = workbook.sheet_by_index(0)

for c in range(sheet.ncols):
    for r in range(sheet.nrows):
    st = (sheet.cell_value(r, c))
    try:
        if st == float(st):
            st_string = str(int(st))
            xpath = "//input[@value='Analyze' and contains(@onclick,'" + st_string + "')]"
            # cont = 0
            if driver.find_elements_by_xpath(xpath):
                driver.find_element_by_xpath(xpath).click()  # Here new window will open
                handles = driver.window_handles
                h = len(handles)  # h=2

                # print(handles)
                for handle in range(1, h):
                    # for handle in range(h):

                    driver.switch_to.window(handles[handle])
                    driver.find_element_by_xpath(
                        '/html/body/table/tbody/tr[4]/td/table/tbody/tr[9]/td[3]/input').click()
                    time.sleep(2)
                    driver.close()

                    lst_window = driver.window_handles
                    h2 = len(lst_window)  # h2=3

                    def check_exists_by_xpath(xpath):
                        try:
                            webdriver.find_element_by_xpath(xpath)
                        except NoSuchElementException:
                            return False
                        return True

                    # for lst_handle in range(2,h2):
                    for lst_handle in range(1, h2):
                        driver.switch_to.window(lst_window[lst_handle])
                        xpath2 = '//*[@id="create_button"]'
                        xpath3 = '//*[@id="update_button"]'


                        if check_exists_by_xpath(xpath2):
                            driver.find_element_by_xpath(xpath2).click()
                            driver.close()
                        elif check_exists_by_xpath(xpath3):
                            driver.find_element_by_xpath(xpath3).click()
                            driver.close()

            # cont+=1
        # print(cont)



    # except ValueError:
    except Exception:
        if st == str(st):
            xpath = "//input[@value='Analyze' and contains(@onclick,'" + st + "')]"
            # cont1=0
            if driver.find_elements_by_xpath(xpath):
                driver.find_element_by_xpath(xpath).click()  # Here new window will open
                handles = driver.window_handles
                h = len(handles)  # h=2

                # print(handles)
                for handle in range(1, h):
                    # for handle in range(h):

                    driver.switch_to.window(
                        handles[handle])  # /html/body/table/tbody/tr[4]/td/table/tbody/tr[9]/td[3]/input
                    driver.find_element_by_xpath(
                        '/html/body/table/tbody/tr[4]/td/table/tbody/tr[9]/td[3]/input').click()
                    # time.sleep(5)
                    driver.close()

                    lst_window = driver.window_handles
                    h2 = len(lst_window)  # h2=2

                    for lst_handle in range(2, h2):
                        if driver.switch_to.window(lst_window[lst_handle]):
                            driver.find_element_by_xpath('//*[@id="create_button"]').click()
                            driver.close()
                        else:
                            driver.find_element_by_xpath('//*[@id="update_button"]').click()
                            driver.close()

看起来您当前的问题很简单,您正试图将带有字母字符的字符串转换为需要严格数字值的浮点。excel工作表上的任何数据都无法转换为数字。

您能上传所收到错误的全文吗?我有点困惑到底是什么问题。另外,由于xPath有问题,您可以上传HTML或URL链接吗?最后,在哪里定义了
driver
?例如,如果您正在使用Chrome,则需要以这种方式定义驱动程序:
driver=webdriver.Chrome(executable_path=“/path/to/binary/chromedriver”)在第二次检查中,我不知道您想做什么。您首先在哪里打开浏览器?Selenium函数,如通过xPath查找内容,需要打开web浏览器,因为这是Selenium唯一可以与之交互的东西。在路径顶部有一些代码,我无论如何都不能共享。我认为,如果没有完整的HTML或页面URL,就不可能调试xPath。最后,你能粘贴你得到的错误的全文吗?没有得到任何错误。进程以退出代码0结束。我不知道在哪里定义check_exists_by_xpath(xpath)函数。所以也发布我的前半段代码。从selenium导入webdriver从selenium.common.exceptions导入NoSuchElementException导入xlrd driver=webdriver.Chrome(r'C:\Users\tmou\PycharmProjects\Python\WebScraping\chromedriver.exe)driver.maximize_window()driver.get('))driver.get('))driver.switch_to.frame('ST_mainFrame'))emailElem=driver.通过xpath('/*[@id=“log\u main”]/input')emailElem.send\u key('xshdfg@xyz.com“)passwordElem=driver.find_element_by_xpath('/*[@id=“log\u main”]/p[1]/input')passwordElem.send_key('/*.####passwd=passwordElem.find_element_by_xpath('/*[@id=“log\u main”]/p[2]/input')。单击()驱动程序。切换到父框架()驱动程序。切换到.frame('topFrame')驱动程序。通过css选择器('.menu>ul:nth child(1)>li:nth child(7)>a:nth child(1)>span:nth child(1)')查找元素。单击()报告驱动程序。切换到.parent\u frame()驱动程序。切换到.frame('stu mainFrame')驱动程序。通过css选择器('div.menu行:nth child(16)>span:nth child(3)>标签:nth child(2)查找元素.click()#FTO driver.切换到.frame('FTO_details')驱动程序。通过css选择器('html body table tbody tr td form table tbody tr td a img')查找元素。单击()#如果通过xpath(xpath2)
检查是否存在的右上方定义元素,则搜索应该有效。您将希望import语句与其他语句一起位于文件的顶部。你能在你的问题中加上那个代码吗?在这样的注释中阅读是非常困难的。Excel包含如下值:1615525 1652273 4C17461624 1649059 Xapth:xpath=“//输入[@value='Analyze'和contains(@onclick,“+st_string+”)”将只接受字符串,因此我尝试将它们转换为字符串。如果您只想将它们转换为字符串,为什么不简单地使用st_string=str(st)?我想您可以这样做,但第53行也有“st==float(st)”,我相信这会导致您当前的错误。是的,因为Excel值是整数和字符串,当我们在代码中使用Excel时,整数将被视为浮点数,所以我尝试将它们转换为INT,然后转换为STR。我直接转换为字符串,但是.0将在结尾处,我不希望出现。#C.Peck让你现在检查一下。
from selenium import webdriver
import time
from selenium.common.exceptions import NoSuchElementException
import xlrd

driver = webdriver.Chrome(r'C:\Users\tmou\PycharmProjects\Python\WebScraping\chromedriver.exe')
driver.maximize_window()
driver.get('https://www.stew.cyz.com/st/st_main.php')
driver.switch_to.frame('ST_mainFrame')
emailElem = driver.find_element_by_xpath('//*[@id="log_main"]/input')

# emailElem=browser.find_element_by_id('log_main')
# emailElem=browser.find_element_by_name("hpuser_id")
emailElem.send_keys('xyn@hyo.com')
# time.sleep(5)
passwordElem = driver.find_element_by_xpath('//*[@id="log_main"]/p[1]/input')
passwordElem.send_keys('********')
passwd = passwordElem.find_element_by_xpath('//*[@id="log_main"]/p[2]/input').click()
driver.switch_to.parent_frame()
driver.switch_to.frame('topFrame')

driver.find_element_by_css_selector(
'.menu > ul:nth-child(1) > li:nth-child(7) > a:nth-child(1) > span:nth-child(1)').click()  # Reports
driver.switch_to.parent_frame()
driver.switch_to.frame('ST_mainFrame')
driver.find_element_by_css_selector(
'div.menu_row:nth-child(16) > span:nth-child(3) > label:nth-child(2)').click()  # FTO

driver.switch_to.frame('fto_details')
time.sleep(8)
driver.find_element_by_css_selector('html body table tbody tr td form table tbody tr td a img').click()  # search


path = r'C:\Users\tmou\PycharmProjects\Python\WebScraping\Book2.xlsx'
workbook = xlrd.open_workbook(path)
sheet = workbook.sheet_by_index(0)

for c in range(sheet.ncols):
    for r in range(sheet.nrows):
    st = (sheet.cell_value(r, c))
    try:
        if st == float(st):
            st_string = str(int(st))
            xpath = "//input[@value='Analyze' and contains(@onclick,'" + st_string + "')]"
            # cont = 0
            if driver.find_elements_by_xpath(xpath):
                driver.find_element_by_xpath(xpath).click()  # Here new window will open
                handles = driver.window_handles
                h = len(handles)  # h=2

                # print(handles)
                for handle in range(1, h):
                    # for handle in range(h):

                    driver.switch_to.window(handles[handle])
                    driver.find_element_by_xpath(
                        '/html/body/table/tbody/tr[4]/td/table/tbody/tr[9]/td[3]/input').click()
                    time.sleep(2)
                    driver.close()

                    lst_window = driver.window_handles
                    h2 = len(lst_window)  # h2=3

                    def check_exists_by_xpath(xpath):
                        try:
                            webdriver.find_element_by_xpath(xpath)
                        except NoSuchElementException:
                            return False
                        return True

                    # for lst_handle in range(2,h2):
                    for lst_handle in range(1, h2):
                        driver.switch_to.window(lst_window[lst_handle])
                        xpath2 = '//*[@id="create_button"]'
                        xpath3 = '//*[@id="update_button"]'


                        if check_exists_by_xpath(xpath2):
                            driver.find_element_by_xpath(xpath2).click()
                            driver.close()
                        elif check_exists_by_xpath(xpath3):
                            driver.find_element_by_xpath(xpath3).click()
                            driver.close()

            # cont+=1
        # print(cont)



    # except ValueError:
    except Exception:
        if st == str(st):
            xpath = "//input[@value='Analyze' and contains(@onclick,'" + st + "')]"
            # cont1=0
            if driver.find_elements_by_xpath(xpath):
                driver.find_element_by_xpath(xpath).click()  # Here new window will open
                handles = driver.window_handles
                h = len(handles)  # h=2

                # print(handles)
                for handle in range(1, h):
                    # for handle in range(h):

                    driver.switch_to.window(
                        handles[handle])  # /html/body/table/tbody/tr[4]/td/table/tbody/tr[9]/td[3]/input
                    driver.find_element_by_xpath(
                        '/html/body/table/tbody/tr[4]/td/table/tbody/tr[9]/td[3]/input').click()
                    # time.sleep(5)
                    driver.close()

                    lst_window = driver.window_handles
                    h2 = len(lst_window)  # h2=2

                    for lst_handle in range(2, h2):
                        if driver.switch_to.window(lst_window[lst_handle]):
                            driver.find_element_by_xpath('//*[@id="create_button"]').click()
                            driver.close()
                        else:
                            driver.find_element_by_xpath('//*[@id="update_button"]').click()
                            driver.close()