Python Selenium无法点击一个按钮,但可以点击另一个按钮。

huangapple go评论77阅读模式
英文:

python selenium not clicking on one button but clicks on another

问题

这是我尝试使用Selenium自动化的网站 ,但它只能点击表格中规格选项卡下最后两个带有加号图标的项目。

这是我尝试的代码:

def fun_trade_products(trade_product):
    trade_products = WebDriverWait(driver, delay).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "di-cl.ewr-ca")))
    for j in trade_products:
        if j.text in trade_product:

            #driver.execute_script("arguments[0].scrollIntoView();", j) 
            print(j.text, "------------------------->")
            plusclick = WebDriverWait(j, delay).until(EC.presence_of_element_located((By.TAG_NAME, "td")))
            sleep(2)
            #driver.execute_script("arguments[0].scrollIntoView();", plusclick) 
            plusclick.click()
            sleep(3)
            return None
product_list = ['I - Live animals; animal products', 'II - Vegetable products', 'III - Fats and oils', 'IV - Prepared foodstuffs', 'V - Mineral products', 'VI - Products of the chemical industry', 'VII - Plastics and rubber and articles thereof', 'VIII - Raw hides and skins, articles thereof', 'IX - Wood and articles of wood', 'X - Pulp of wood, paper, paperboard and articles thereof', 'XI - Textiles and textile articles', 'XII - Footwear, headgear, etc.', 'XIII - Articles of stone, ceramic products, glass', 'XIV - Pearls, precious stones and metals, articles thereof', 'XV - Base metals and articles thereof', 'XVI - Machinery and mechanical appliances, electrical and electrotechnical equipment', 'XVII - Transport equipment', 'XVIII - Optical, photographic, measuring, checking instruments, etc.', 'XIX - Arms and ammunition', 'XX - Miscellaneous manufactured articles', "XXI - Works of art, collectors' pieces and antiques"]

for i in range(len(product_list)):
    fun_trade_products(product_list[i])

我想要点击“product_list”列表中的每个项目在网站上的加号按钮。

由于网站在每次点击后都会更新,因此有时会出现元素失效的异常错误,所以我将整个主要代码放在一个函数下,对于“product_list”中的每个项目,“trade_products”变量会得到更新,我就可以点击网站上的加号按钮。

但是,我的代码可以点击除了最后两个('XX - Miscellaneous manufactured articles' 和 'XXI - Works of art, collectors' pieces and antiques')之外的所有加号按钮,我不知道为什么。

有没有办法也点击最后两个?

谢谢,如果英语不好请见谅。

英文:

So, this is a website i am trying to automate using selenium. But, it's not clicking on only the last two items with a plus image button in the table under the specification tab.

This is the code i tried:

def fun_trade_products(trade_product):
    trade_products = WebDriverWait(driver, delay).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "di-cl.ewr-ca")))
    for j in trade_products:
        if j.text in trade_product:

            #driver.execute_script("arguments[0].scrollIntoView();", j) 
            print(j.text, "------------------------->")
            plusclick = WebDriverWait(j, delay).until(EC.presence_of_element_located((By.TAG_NAME, "td")))
            sleep(2)
            #driver.execute_script("arguments[0].scrollIntoView();", plusclick) 
            plusclick.click()
            sleep(3)
            return None
product_list = ['I - Live animals; animal products', 'II - Vegetable products', 'III - Fats and oils', 'IV - Prepared foodstuffs', 'V - Mineral products', 'VI - Products of the chemical industry', 'VII - Plastics and rubber and articles thereof', 'VIII - Raw hides and skins, articles thereof', 'IX - Wood and articles of wood', 'X - Pulp of wood, paper, paperboard and articles thereof', 'XI - Textiles and textile articles', 'XII - Footwear, headgear, etc.', 'XIII - Articles of stone, ceramic products, glass', 'XIV - Pearls, precious stones and metals, articles thereof', 'XV - Base metals and articles thereof', 'XVI - Machinery and mechanical appliances, electrical and electrotechnical equipment', 'XVII - Transport equipment', 'XVIII - Optical, photographic, measuring, checking instruments, etc.', 'XIX - Arms and ammunition', 'XX - Miscellaneous manufactured articles', "XXI - Works of art, collectors' pieces and antiques"]

for i in range(len(product_list)):
    fun_trade_products(product_list[i])

I want to click on the plus button in the website of every item in the list 'product list'.

Since, the website updates after every click, It gives me a stale element exception error sometimes, so i put the whole main code under a function and for every item in the product_list, the variable 'trade_products' gets updated and i can click on the plus button in the website.

But, my code is clicking on every plus button except only the last two ('XX - Miscellaneous manufactured articles', "XXI - Works of art, collectors' pieces and antiques") and i don't know why.

Is there anyway to click on the last two too?

Thank you and sorry if the English is not good.

答案1

得分: 0

# 等待浏览器加载完毕
WebDriverWait(driver, 6).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.ewr-nglr input[title="Expand"]')))

# 循环扩展所有行
while True:
    # 检查表格中是否存在可扩展的元素,并双击父行
    if not driver.execute_script(
        "p=document.querySelectorAll('div.ewr-nglr input[title=\"Expand\"]')[2]; "
        "if (p) {return p.closest('div.di-cl.ewr-ca').dispatchEvent(new MouseEvent('dblclick'))} "
        "else {return false}"
    ):
        break
    # 等待直到新数据被添加
    WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.CLASS_NAME, 'ewa-background-ready')))
    # 滚动表格;出于某种原因需要这样做以避免错误
    driver.execute_script('table=document.querySelector("#ctl00_SPWebPartManager1_g_6b9d1cc9_04bc_403b_934a_b940ef65bd11_ctl01_ctl00_sheetContentDiv"); table.scrollTop=table.scrollHeight')
英文:

I am assuming that you want to expand all the rows in this url. Here is one way to do it.

# first wait till browser loads
WebDriverWait(driver, 6).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.ewr-nglr input[title="Expand"]')))

# loop across all expandable
while True:
    # checking if expandable/+ elements exist inside table and double clicking on parent row
    if not driver.execute_script("\
        p=document.querySelectorAll('div.ewr-nglr input[title=\"Expand\"]')[2]; \
        if (p) {return p.closest('div.di-cl.ewr-ca').dispatchEvent(new MouseEvent('dblclick'))} \
        else {return false}") : break
    # waiting till new data is added
    WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.CLASS_NAME, 'ewa-background-ready')))
    # scrolling the table; needed for some reason to avoid error
    driver.execute_script('table=document.querySelector("#ctl00_SPWebPartManager1_g_6b9d1cc9_04bc_403b_934a_b940ef65bd11_ctl01_ctl00_sheetContentDiv"); \
        table.scrollTop=table.scrollHeight')

I couldn't get the click button to work so I used double click on the row. Press Alt+DownArrow also worked but with that approach I would had to write extra code for filtering expandable row.

huangapple
  • 本文由 发表于 2023年7月11日 00:03:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定