Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.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
在BeautifulSoup Python上查找Botton时出现问题_Python_Beautifulsoup - Fatal编程技术网

在BeautifulSoup Python上查找Botton时出现问题

在BeautifulSoup Python上查找Botton时出现问题,python,beautifulsoup,Python,Beautifulsoup,我试图单击网页上的按钮,但找不到href。我的代码如下: from bs4 import BeautifulSoup from selenium import webdriver browser = webdriver.Chrome() ref = 'https://www.leychile.cl/Consulta/buscador_experto' browser.get(ref) python_button = browser.find_element_by_xpath("//inp

我试图单击网页上的按钮,但找不到href。我的代码如下:

from bs4 import BeautifulSoup
from selenium import webdriver

browser = webdriver.Chrome() 
ref = 'https://www.leychile.cl/Consulta/buscador_experto'

browser.get(ref)

python_button = browser.find_element_by_xpath("//input[@type='button'][@value='Buscar']")
search_box = browser.find_element_by_name("palabra_frase")
search_box.send_keys("Alcaldesa")
time.sleep(1)
python_button.click()
page = BeautifulSoup(browser.page_source) 
continue_link = page.find_element_by_link_text('Siguiente')
browser = webdriver.Chrome()
ref = 'https://www.leychile.cl/Consulta/buscador_experto'

browser.get(ref)

python_button = browser.find_element_by_xpath("//input[@type='button'][@value='Buscar']")
search_box = browser.find_element_by_name("palabra_frase")
search_box.send_keys("Alcaldesa")
time.sleep(1)
python_button.click()
browser.execute_script("javascript:Paginar2(2)")
在这个新网页中,我试图点击“Siguiente”按钮

当我查看网页时,有以下href

<a href="javascript:Paginar2(87)"> Siguiente › </a>


但是BeautifulSoup没有显示该按钮,因此我无法进行单击。

要更改页面,可以执行以下操作:

from bs4 import BeautifulSoup
from selenium import webdriver

browser = webdriver.Chrome() 
ref = 'https://www.leychile.cl/Consulta/buscador_experto'

browser.get(ref)

python_button = browser.find_element_by_xpath("//input[@type='button'][@value='Buscar']")
search_box = browser.find_element_by_name("palabra_frase")
search_box.send_keys("Alcaldesa")
time.sleep(1)
python_button.click()
page = BeautifulSoup(browser.page_source) 
continue_link = page.find_element_by_link_text('Siguiente')
browser = webdriver.Chrome()
ref = 'https://www.leychile.cl/Consulta/buscador_experto'

browser.get(ref)

python_button = browser.find_element_by_xpath("//input[@type='button'][@value='Buscar']")
search_box = browser.find_element_by_name("palabra_frase")
search_box.send_keys("Alcaldesa")
time.sleep(1)
python_button.click()
browser.execute_script("javascript:Paginar2(2)")
说明: 当您检查代码时,您会看到有一个javascript脚本可以更改这部分代码中的页面:

<span class="avanzar_resultados" name="paginador2" nitemsporpagina="10" 
pagina="1" totalitem="1781">1 - 10 de 1781<a href="javascript:Paginar2(2)"> 
Siguiente › </a><a href="javascript:Paginar2(179)"> Última » </a></span>
测试: 如果您随后运行:

page = BeautifulSoup(browser.page_source)
page.find('span', {'class':'uppercase'}).text

您得到的是
'\n删除到743 EXENTO\n'
(第2页)而不是
'\n解决方案65 EXENTA\n'
(第1页)

这不是按钮,而是锚定。它与代码中的XPath有什么关系?您正在寻找带有Selenium的按钮,而不是BS。我代码中的XPath只是指向我实际想要抓取的网页,即带有搜索结果“Alcaldesa”的网页,好的,但是,在转到下一页后,您需要发布试图查找
Siguiente
锚定的代码。