使用硒填写带有随机数的表格

时间:2018-07-30 09:04:17

标签: python-3.x selenium

我在填写表格时遇到麻烦,该表格使用java创建了一对随机数,在输入正确答案后可以继续:

<div class="form-group has-feedback">
<label for="captcha" class="..." id="captchaOperation">6 + 20 =</label>

我想提取两个数字,然后将它们相加(主要是求和运算,但也欢迎使用通用解决方案)。

我对send_keys使用了不同的组合,例如:

 mathcaptcha = action.send_keys("""document.querySelectorAll("span#captchaOperation").firstChild.nodeValue;""")
 print(mathcaptcha) 

全部返回:

<selenium.webdriver.common.action_chains.ActionChains object at 0x11238cd68>

然后我将execute_script与几个不同的脚本一起使用,例如:

mathcaptcha = driver.execute_script("""document.getElementById("captchaOperation");""")
print(mathcaptcha)

并打印出None。 如何从代码中获得6 + 20或最终结果26

3 个答案:

答案 0 :(得分:2)

要使execute_script向您返回一个值,请使用return

mathcaptha = driver.execute_script("return document.getElementById('captchaOperation').textContent;")
print(mathcaptha)

答案 1 :(得分:1)

您可以尝试以下代码:

    $products = Product::when($request->filled('brand'), function ($query)use($request) {
    $brandRequest = $request->input('brand');
    $brandReqArray = explode(',', $brandRequest);
    $query->whereHas('brands', function($q) use ($brandReqArray)  {
        $q->whereIn('slug', $brandReqArray);
    });
})->when($request->filled('gender'), function ($query)use($request) {
    $genderRequest = $request->input('gender');
    $genderReqArray = explode(',', $genderRequest);
    $query->whereHas('genders', function($q) use ($genderReqArray)  {
        $q->whereIn('slug', $genderReqArray);
    });
})->when($request->filled('price'), function ($query)use($request) {
    $priceRequest = $request->input('price');
    $priceReqArray = explode(';', $priceRequest);
    $query->whereHas('deals', function($q) use ($priceReqArray)  {
        $q->whereBetween('price', $priceReqArray)
        ->whereNotExists(function ($q)use($priceReqArray) {
            $q->select(DB::raw(1))
                ->from('deals')  
                //->whereNull('deals.deleted_at')                     
                ->whereNotBetween('price', $priceReqArray)
            ->whereRaw('product.id = deals.productId');
        }) 
        ->orderBy('price','asc')->limit(1);
    })
})->paginate(24);

答案 2 :(得分:1)

根据您共享的 HTML 提取 6 + 20 ,您可以使用以下任一解决方案:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
#other lines of code
captcha_text = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//label[@id='captchaOperation' and @for='captcha']"))).get_attribute("innerHTML")
print((captcha_text.replace("=", "")), eval(captcha_text.replace("=", "")))