Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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 selenium单击a href按钮_Python_Selenium_Web Crawler - Fatal编程技术网

Python selenium单击a href按钮

Python selenium单击a href按钮,python,selenium,web-crawler,Python,Selenium,Web Crawler,我对硒真的很陌生。 目前,我正在尝试使用selenium和beautifulsoup进行一些网络绘图。我正在网上浏览的网站是https://bigd.big.ac.cn/dogsdv2/pages/modules/indsnp/indsnp_search.jsp。 这是我现在拥有的代码 driver=webdriver.Chrome(可执行文件路径=路径到chromebrowser) 驱动程序。获取(“https://bigd.big.ac.cn/dogsdv2/pages/modules/in

我对硒真的很陌生。 目前,我正在尝试使用selenium和beautifulsoup进行一些网络绘图。我正在网上浏览的网站是
https://bigd.big.ac.cn/dogsdv2/pages/modules/indsnp/indsnp_search.jsp
。 这是我现在拥有的代码

driver=webdriver.Chrome(可执行文件路径=路径到chromebrowser)
驱动程序。获取(“https://bigd.big.ac.cn/dogsdv2/pages/modules/indsnp/indsnp_search.jsp")
输入\区域=驱动程序。按\名称查找\元素(“searchForm.genename”)
输入\u区域。发送\u键(“P2RY12”)
searcher=驱动程序。通过类名称(“按钮”)查找元素
searcher.click()
#table=驱动程序。按类名称查找元素(“table7 table7 border”)
#表。按标签名称(“a”)查找元素。单击()
我试图点击搜索时出现的第一个SNP ID。单击搜索结果href的好方法是什么?

试试以下方法:

firstsnpID = driver.find_element_by_xpath("(.//table[@class='table7 table7-border']/tbody/tr/td[3]/a)[1]")
firstsnpID.click()

您不能使用复合类通过
find_element_by_class_name

单击表上的第一个链接来定位元素,并使用
WebDriverWait
()和
element_to_be_clickable
()以及下面的CSS选择器

代码

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome(executable_path=path_to_chromebrowser)
driver.get("https://bigd.big.ac.cn/dogsdv2/pages/modules/indsnp/indsnp_search.jsp")
input_area = driver.find_element_by_name("searchForm.genename")
input_area.send_keys("P2RY12")
searcher = driver.find_element_by_class_name("button")
searcher.click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"table.table7.table7-border td>a[href^='/dogsdv2/refsnp/showRefSNPDetail']"))).click()

要获取所有链接,请导入位于的所有元素的
WebDriverWait
()和
可见性,\u并获取href值,然后迭代每个url

allelemets=WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,"table.table7.table7-border td>a[href^='/dogsdv2/refsnp/showRefSNPDetail']")))

allurls=[item.get_attribute('href') for item in allelemets]
print(allurls)
for link in allurls:
   driver.get(link)

driver。通过xpath('/html/body/div/div[2]/div[2]/form/table/tbody/tr[1]/td[3]/a[1]'查找元素。单击()

如果您需要其他ID:
用于范围(1,10)内的id:
驱动程序。通过xpath('/html/body/div/div[2]/div[2]/form/table/tbody/tr[{}]/td[3]/a[1]'查找元素。格式(id))。单击()
睡眠(5)

在网页
https://bigd.big.ac.cn/dogsdv2/pages/modules/indsnp/indsnp_search.jsp
要搜索基因名为P2RY12并单击搜索时出现的第一个SNP ID,您需要引导WebDriverWait使
元素可单击()
,您可以使用以下方法:

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    
    driver.get('https://bigd.big.ac.cn/dogsdv2/pages/modules/indsnp/indsnp_search.jsp')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#idgname[name='searchForm.genename']"))).send_keys("P2RY12")
    driver.find_element_by_css_selector("button.button[type='submit']").click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "form[action^='/dogsdv2/com/exportFile'] table>tbody>tr td:nth-child(3)>a"))).click()
    
  • 浏览器快照: