Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.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/8/selenium/4.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
使用Selenium、Python检查文本是否可见_Python_Selenium_Element_Visible - Fatal编程技术网

使用Selenium、Python检查文本是否可见

使用Selenium、Python检查文本是否可见,python,selenium,element,visible,Python,Selenium,Element,Visible,我正在制作一个脚本,它将填充表单,用Python创建一个Gmail帐户。当用户名已被使用时,消息将可见。荷兰语:“Dez gebruikersnaam Best al gebruikt.Probeer Beer Ander gebruikersnaam.”为了能够处理这一问题,我需要检测此消息何时可见,但我不知道如何做到这一点。如果有人能帮上忙,那就太棒了 试试这个代码: from selenium import webdriver import time path = '/home/avio

我正在制作一个脚本,它将填充表单,用Python创建一个Gmail帐户。当用户名已被使用时,消息将可见。荷兰语:“Dez gebruikersnaam Best al gebruikt.Probeer Beer Ander gebruikersnaam.”为了能够处理这一问题,我需要检测此消息何时可见,但我不知道如何做到这一点。如果有人能帮上忙,那就太棒了


试试这个代码

from selenium import webdriver
import time

path = '/home/avionerman/Documents/stack'
driver = webdriver.Firefox(path)

driver.get('https://www.google.com/intl/nl/gmail/about/#')

try:
    print('locating create account button')
    create_account_button = driver.find_element_by_class_name('h-c-button')
except:
    print("error, couldn't find create account button")
try:
    create_account_button.click()
    print('navigating to creation page')
except:
    print('error navigating to creation page')
time.sleep(8)


handles = driver.window_handles
size = len(handles)

driver.switch_to.window(handles[1])
print(driver.title)



first_name_input = driver.find_element_by_id('firstName')
first_name_input.click()
first_name_input.send_keys("WhateverYouWant")

last_name_input = driver.find_element_by_id('lastName')
last_name_input.click()
last_name_input.send_keys("WhateverYouWant2")

username_input = driver.find_element_by_id('username')
username_input.click()
username_input.send_keys('papadopoulos')

pswd_input = driver.find_element_by_name('Passwd')
pswd_input.click()
pswd_input.send_keys('whateveryouwant')

pswd_conf_input = driver.find_element_by_name('ConfirmPasswd')
pswd_conf_input.click()
pswd_conf_input.send_keys('whateveryouwant')

next_button = driver.find_element_by_id("accountDetailsNext")
next_button.click()


# In the variable x I save the text that is included inside the specific xpath
x = driver.find_element_by_xpath("//*[@id='view_container']/form/div[2]/div/div[1]/div[2]/div[1]/div/div[2]/div[2]/div").text
print(x)

# I assert in order to check if the variable x has the proper text value (the expected one)
assert x == 'Dez gebruikersnaam worst al gebruikt. Probeer been andere gebruikersnaam.'


time.sleep(10)

这样,如果断言通过,则表示您看到警告消息,并且在任何其他情况下,邮件的用户名都是唯一的。所以,我给你写的代码部分,你可以把它插入if语句中,你可以随心所欲地处理它。如果您需要更多信息,请随时与我联系。

查看我的答案。这对您有用吗?driver.find_元素(By.XPATH,“//div[@jsname='B34EJ']).text,并可以相应地应用验证。插入新的XPATH。你能再试一次吗?如果这对你有效,你能接受它作为解决方案吗?:)由于某些原因,它不起作用-消息:没有这样的元素:无法找到元素:{“方法”:“xpath”,“选择器”:/html/body/div[1]/div/div[2]/div[1]/div[2]/form/div[2]/div/div[1]/div/div/div[2]/div[2]/div”}无论我使用什么路径或元素选择器,都会得到这个结果,因为它在我的机器上运行,所以这很奇怪。嗯,你能(手动)搜索代码中的XPath吗?我要你告诉我它存在于平台的代码中。好吗?你也可以试着计时。在你尝试定位元素之前,请先睡(1)一秒钟。没关系,它成功了,我只是犯了一个小错误,再次感谢你的帮助!如果这对你有效,你能接受它作为解决方案吗?:)