簡體   English   中英

selenium 無法使用方法定位元素:無法通過 id、css_selector、xpath、鏈接文本找到元素

[英]selenium Unable to locate element using methods: cannot find elements by id, css_selector, xpath, link text

我正在嘗試抓取數據庫中的數據。 我之前確實問過一個類似的問題,但我目前的問題是具體的/我開始更多地了解這個問題。

到目前為止,使用 selenium,我可以在“按植物性食物搜索”字段中輸入 22663,然后單擊下方的“食物-疾病關聯”,然后單擊提交,如下所示:

在此處輸入圖片說明

這是我遇到問題的下一頁,我無法單擊“植物疾病關聯”。

我已經嘗試了其他 SO 帖子中的許多想法:

import sys
import pandas as pd
from bs4 import BeautifulSoup
import selenium
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import csv
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.by import By

#binary = FirefoxBinary('/Users/kela/Desktop/scripts/scraping/geckodriver')
url = 'http://147.8.185.62/services/NutriChem-2.0/'
driver = webdriver.Firefox(executable_path='/Users/kela/Desktop/scripts/scraping/geckodriver')
driver.get(url)

#input the tax ID
element = driver.find_element_by_id("input_food_name")
element.send_keys("22663")

#click food-disease association
element = Select(driver.find_element_by_css_selector('[name=food_search_section]'))
element.select_by_value('food_disease')

#click submit
submit_xpath = '/html/body/form/p[2]/input[1]'
destination_page_link = driver.find_element_by_xpath(submit_xpath)
destination_page_link.click()


# this is where it goes wrong
#click plant-disease associations
#table_data = driver.find_elements_by_xpath('//td[@class="likeabutton"]')
#driver.find_element_by_link_text("plant-disease").click()
#driver.find_element_by_link_text("nutrichem12587_disease.tsv").click()


 #driver.find_element_by_xpath("//div[contains(@onclick'nutrichem12587_disease.tsv']").click()

#values = []
#for i in table_data.find_element_by_tag_name('Plant-Disease associations'):
#    values.append(i.text)
#print(value)


#span = table_data.find_element_by_tag_name('Plant-Disease associations')
#print(span)

#select = Select(driver.find_element_by_xpath("/html/body/table/tbody/tr/td[3]"))
#select.click()
#submit_xpath = '/html/body/table/tbody/tr/td[3]/div/span'
#submit_xpath = '/html/body/table/tbody/tr/td[3]'
#destination_page_link = driver.find_element_by_xpath(submit_xpath)
#destination_page_link.click()

#element = driver.find_element_by_xpath("//select[@name='plant-disease']")
#element.select_by_value('Plant-Disease associations')


#xpath2 = '/html/body/table/tbody/tr/td[3]/div'
#destination_page_link = driver.find_element_by_xpath(xpath2)
#destination_page_link.click()

#xpath2 = '/html/body/table/tbody/tr/td[3]/div/span'
#destination_page_link = driver.find_element_by_xpath(xpath2)
#destination_page_link.click()

我已經注釋掉了我嘗試過但不起作用的所有行。 您可以看到我嘗試了不同 SO 帖子中建議的多個選項,我知道那里有很多類似的問題,但似乎沒有一個解決方案對我有用; 所有錯誤基本相同,'找不到元素'(例如 selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: nutrichem12587_disease.tsv)

有人可以幫我點擊“植物疾病協會”按鈕。 我想知道,是不是因為我試圖點擊的頁面是 .php?

它在一個框架內。 你需要切換到那個

driver.find_element_by_css_selector('[value="Submit"]').click()
driver.switch_to.frame(driver.find_element_by_css_selector('frame'))
driver.find_element_by_css_selector('[onclick*="plant-disease"]').click()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM