我正在尝试在下面的链接中输入CEP,然后按"计算"按钮,由标签和类型"按钮给出。"我认为我已经确定机械化不能完成工作,但即使是硒也没有做我需要的工作。下面是我的机械化代码。
br = mechanize.Browser()
br.set_handle_robots(False)
br.set_handle_refresh(False)
br.set_all_readonly(False)
br.set_debug_http(True)
br.set_debug_responses(True)
br.addheaders=[('User-Agent', 'Mozilla/5.0 (X11; Linux x86_64)]
br.open(prod_url)
print br.title()
print br.geturl()
br.select_form(nr=1)
br['postalCode']='05454-050'
br.submit()
print br.geturl()
sourceCode=BeautifulSoup(br.response().read(),'html.parser')
How to unit test that ExecutorService spawns new thread for task?
答案 0 :(得分:0)
使用selenium,你需要清除占位符然后用send_keys
输入邮政编码文本,以获得frete价格等。我们需要等到该元素可见::
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def wait(dr, x, t):
element = WebDriverWait(dr, t).until(
EC.presence_of_element_located((By.CSS_SELECTOR, x))
)
return element
dr = webdriver.Firefox()
dr.maximize_window()
dr.get("http://www.americanas.com.br/produto/113266366/fone-de-ouvido-philips-over-ear-branco-shl3000wt")
inp = dr.find_element_by_id("postalCode")
inp.clear()
inp.send_keys("05454050")
inp.send_keys(Keys.RETURN)
ele = wait(dr, "table.tbl-freight-template tr td span.freight-value.info-freight", 20)
print(ele.text)
运行上面的代码将输出:
R$ 18,45