我在Python上使用Selenium遇到了一個小問題。 在我的主腳本中,下面是(右下),我只想設法執行另一個外部python腳本。無法使用Selenium導入外部Python程序
import time, os
import re #regex
import uuid
import urllib
import subprocess, sys
import pyautogui
import PIL
from PIL import Image
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
def download():
urllib.urlretrieve(m, "images/imgOUI_"+unique_filename+".png")
im = Image.open("images/imgOUI_"+unique_filename+".png")
out = im.resize((int(ExtractResize), int(ExtractResize2)))
out.save("images/ImgComplete_"+unique_filename+".png")
co = webdriver.ChromeOptions()
co.add_argument("--user-data-dir=C:\Users\Marshall\AppData\Local\Google\Chrome\User Data\Default")
browser = webdriver.Chrome(chrome_options = co)
browser.get("http://*/")
browser.find_element_by_id('SubmitButton').click()
#----------Move iframe-------
try:
wait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it(browser.find_element_by_xpath('//iframe[contains(@src, "google.com/recaptcha")]')))
except:
print("error")
[...]
while True:
[...]
link = wait(browser, 20).until(EC.presence_of_all_elements_located((By.XPATH, '//img[contains(@src, "https://www.google.com/recaptcha/api2/payload?")]')))
listLink = []
for k in link:
m = k.get_attribute('src')
if m in listLink:
print("Already added")
else:
listLink.insert(0, m)
test = k.value_of_css_property("height")
test2 = k.value_of_css_property("width")
ExtractResize = test.replace('px', '')
ExtractResize2 = test2.replace('px', '')
unique_filename = str(uuid.uuid4())
download()
dim = wait(browser, 20).until(EC.presence_of_element_located((By.XPATH, '//div[contains(@style, "width")]'))).get_attribute('style')
int1, int2 = re.findall(r'\d+', dim)
subprocess.check_call("cut.py", shell=True) #Here, i want execute my other script python
(我用我的代碼的某些時刻,這似乎沒用添加,如果必要的話,我想補充他們!)
這裏cut.py的代碼:
import time
from se import *
import random
import PIL
import uuid
from PIL import Image
def cut():
im = Image.open("images/ImgComplete_"+unique_filename+".png")
left = a
top = b
width = c
height = d
box = (left, top, left+width, top+height)
area = im.crop(box)
print area.size
unique_filename = str(uuid.uuid4())
print unique_filename
area.save("images/Principales/IMGsepare_"+unique_filename+".png")
image_size = (int(ExtractResize), int(ExtractResize2))
portion_size = (int(int1), int(int2))
Test2 = image_size[0]/portion_size[0]
Test = image_size[0]/float(portion_size[0])
List = (0, 0, portion_size[0], portion_size[1])
a, b, c, d = List
while True:
if a==0:
cut()
for mo in range(0, int(Test2)):
a += portion_size[0]
if a == (int(Test2) * portion_size[0]):
break
cut()
if a == (int(Test2) * portion_size[0]):
if not Test2 == Test:
a = (image_size[0] - portion_size[0])
cut()
a = 0
if b == (int(Test2) * portion_size[0]) and not Test2 == Test:
b = (image_size[1] - portion_size[1])
else:
if b == image_size[1]:
print("Break")
break
print b
b += portion_size[0]
if b == (int(Test2) * portion_size[0]) and not Test2 == Test:
b = (image_size[1] - portion_size[1])
print("Vient de reajuster le B")
print b
if b == image_size[1]:
print("Break")
break
所以我嘗試了幾種方法,如下面的:
- subprocess.call( 「cut.py」,殼=假)
- 使用os.system( 「開始cut.py」)
- 的execfile( 「cut.py」)
在所有這些嘗試,我發現我的程序採取相同的行爲:其實,它只是打開另一個空白的谷歌窗口,然後什麼也沒有發生。
我完全不理解這個問題。
編輯:
因此削減一切總之,你想從你目前的Python文件運行另一個python的文件嗎?你發佈的其他代碼不重要嗎? –
沒錯!但是當我嘗試運行我的其他腳本時,我的腳本打開另一個空白網頁似乎很奇怪......這就是爲什麼我更喜歡鏈接我的代碼 –
cut.py'做什麼? –