简体   繁体   English

selenium 无法使用方法定位元素:无法通过 id、css_selector、xpath、链接文本找到元素

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

I am trying to scrape the data in this DB.我正在尝试抓取数据库中的数据。 I did ask a similar question about this previously, but my current question is specific/I am starting to understand the issue more.我之前确实问过一个类似的问题,但我目前的问题是具体的/我开始更多地了解这个问题。

So far, with selenium, I can type 22663 into the 'search by plant-based food' field, then click 'food-disease associations' underneath, and then click submit, as shown here:到目前为止,使用 selenium,我可以在“按植物性食物搜索”字段中输入 22663,然后单击下方的“食物-疾病关联”,然后单击提交,如下所示:

在此处输入图片说明

it's the next page that I have the issue with, I cannot click 'Plant-Disease Associations'.这是我遇到问题的下一页,我无法单击“植物疾病关联”。

I have tried numerous ideas from other SO posts:我已经尝试了其他 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()

I've commented out all the lines that I've tried and don't work.我已经注释掉了我尝试过但不起作用的所有行。 You can see I've tried multiple options as suggested on different SO posts, I'm aware that there are a lot of similar questions out there, but none of the solutions seem to work for me;您可以看到我尝试了不同 SO 帖子中建议的多个选项,我知道那里有很多类似的问题,但似乎没有一个解决方案对我有用; all the errors are basically the same, 'cannot find element' (eg selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: nutrichem12587_disease.tsv)所有错误基本相同,'找不到元素'(例如 selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: nutrichem12587_disease.tsv)

Can someone please help me click on the 'Plant-Disease association' button.有人可以帮我点击“植物疾病协会”按钮。 I'm wondering, is it because the page that I'm trying to click on is .php?我想知道,是不是因为我试图点击的页面是 .php?

It is inside a frame.它在一个框架内。 You need to switch to that你需要切换到那个

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.

相关问题 无法使用python在硒中使用CSS_SELECTOR定位元素 - Unable to locate the element using CSS_SELECTOR in selenium using python NoSuchElementException:无论我使用哪个选择器(xpath、css_selector、fullXpath 等),Selenium 都无法找到搜索栏 - NoSuchElementException: Selenium unable to locate search bar regardless of which selector I use (xpath, css_selector, fullXpath etc..) Selenium,W​​ebdriver-找不到-xpath / css_selector /…python - Selenium, webdriver - can't find - xpath/css_selector/… python Selenium Python - Text() 适用于 Xpath 但不适用于 CSS_Selector - Selenium Python - Text() works with Xpath but does not with CSS_Selector 无法找到硒中的元素:{“方法”:“ css选择器”,“选择器”:“ [id =” identifierId”]”} - Unable to locate element: {“method”:“css selector”,“selector”:“[id=”identifierId“]”} in selenium 收到消息:没有这样的元素:无法找到元素:{"method":"css selector","selector":"[id="None"]"} Python 使用 selenium 时 - Getting Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="None"]"} Python when using selenium Selenium webdriver chrome,无法使用 CSS 选择器或 XPath 定位按钮 - Selenium webdriver chrome, unable to locate button using CSS selector or XPath 使用 Selenium Python 选择下拉菜单 - 无法定位元素:{“method”:“css selector”,“selector”:"[id= - Selecting drop-down using Selenium Python - Unable to locate element: {“method”:“css selector”,“selector”:"[id= Unable to locate element using Selenium Python 通过 xpath 查找元素 - Unable to locate element using Selenium Python find element by xpath 无法使用 Selenium 中 Xpath 的查找元素来定位元素方法? - Unable to locate element method using find element by Xpath in Selenium?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM