Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.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在打印时跳过行_Python_Python 3.x_Selenium - Fatal编程技术网

Python在打印时跳过行

Python在打印时跳过行,python,python-3.x,selenium,Python,Python 3.x,Selenium,所以,我正在创建一个脚本,它将尝试一些电子邮件,并告诉我它们是否有效,但问题是,它在打印时也会跳过行,但它键入的内容非常好。我给你看看。 我会试试这5封电子邮件 gabriel_su bloodown xdiablo89 sc.orion peanut 它会把这个打印出来 bloodown Is taken sc.orion Is available SHAMS Is taken kjadair12 Is taken super_robin75 Is taken 有些甚至不在我的5行中,“

所以,我正在创建一个脚本,它将尝试一些电子邮件,并告诉我它们是否有效,但问题是,它在打印时也会跳过行,但它键入的内容非常好。我给你看看。 我会试试这5封电子邮件

gabriel_su
bloodown
xdiablo89
sc.orion
peanut
它会把这个打印出来

bloodown
Is taken
sc.orion
 Is available
SHAMS
Is taken
kjadair12
Is taken
super_robin75
Is taken
有些甚至不在我的5行中,“超级机器人75”是我的第10行

from selenium import webdriver
import time

txt = open("emails.txt", 'r')

chrome_path = r"C:\Users\Skid\Desktop\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
for x in range(5):
    driver.get("https://signup.live.com/signup?wa=wsignin1.0&rpsnv=12&ct=1455592987&rver=6.4.6456.0&wp=MBI_SSL_SHARED&wreply=https%3a%2f%2fmail.live.com%2fdefault.aspx%3frru%3dinbox&id=64855&cbcxt=mai&bk=1455592987&uiflavor=web&uaid=947b22b7c0d249f8ab527df4fb06c828&mkt=EN-US&lc=1033&lic=1")


    driver.find_element_by_xpath("""//*[@id="memberNameDomain"]""").click()
    driver.find_element_by_xpath("""//*[@id="CredentialsInputPane"]/fieldset/div[2]/div/div[1]/div/ul/li[2]/a""").click()


    type_into_email = driver.find_element_by_xpath("""//*[@id="MemberName"]""")
    type_into_email.send_keys(txt.readline())


    refresh = driver.find_element_by_xpath("""//*[@id="FirstName"]""").click()
    time.sleep(5)

    error = driver.find_element_by_xpath("""//*[@id="MemberNameError"]""")
    if error.is_displayed():
        print(txt.readline() + "Is taken")
    else:
        print(txt.readline() + " Is available")

您在范围(5)中指定
x
,因此输出为5行是一种正常行为

您应该像下面这样循环:

for x in txt:
   ...
   type_into_email.send_keys(x)
   ...
   if error.is_displayed():
       print(x.strip() + "Is taken")
   else:
       print(x.strip() + " Is available")

说清楚一点,你想把结果放在同一行,对吗?为此,请将打印行更改为
print(txt.readline().strip()+…
以删除换行符

for line in txt.readlines()
循环而不是

for x in range(5)


每次迭代都要调用
txt.readline()
多次;这是有意的吗?您可能希望在txt:中使用类似
的名称,然后在当前调用
txt.readline()的位置使用
name
。请发布一个带有解决方案的答案。我不知道我会把它放在哪里,这是用来代替范围(5)中x的
。这样就去掉了间隔的东西,很好,谢谢。但是,我仍然有我的第一个问题,它每隔一行打印一次调用
readlines()
是不必要的,效率也很低。只需对txt中的行使用
谢谢,工作得很好。我试过了,它只会在键入字符时吐出来,但如果我们能解决这个问题,可能会很好。@chepner提到,您只能在txt中为x键入
if error.is_displayed():
    print(line + "Is taken")
else:
    print(line + " Is available")