简体   繁体   English

Python + Selenium —无法访问网页上的元素

[英]Python + Selenium — Cannot access elements on a webpage

I am trying to access an element in Selenium, but none of the typical methods are working. 我正在尝试访问Selenium中的元素,但是没有一种典型的方法有效。 So far I have tried using every variation of 'find_elements_by_x' with no success. 到目前为止,我已经尝试使用'find_elements_by_x'的每个变体都没有成功。 I also spent about a day looking at various forums, but nothing seems to be working. 我也花了大约一天的时间浏览各种论坛,但似乎没有任何效果。 I recently used Selenium in another successful project, but the same structure is not working for this particular website. 我最近在另一个成功的项目中使用了Selenium,但是对于该特定网站,相同的结构不起作用。 Here is a snippet of the HTML containing the elements I am trying to access: 这是包含我要访问的元素的HTML的代码段:

<input type="text" name="username" id="username" placeholder="Username / 
Email" autocapitalize="off" autocorrect="off" required="" ng-
model="credentials.username" class="ng-pristine ng-invalid ng-invalid-
required">

As is probably obvious, this is a username input for logging in. Below are a few lines I have tried so far that have not worked. 显而易见,这是用于登录的用户名输入。下面是我到目前为止尝试过的几行,这些行都没有用。

from selenium import webdriver


driver = webdriver.Chrome("chromedriver.exe filepath")
driver.get('url')

username = driver.find_element_by_xpath('//input[@id="username"]')

That XPath navigates to the element in question if I search for it using the console in Chrome. 如果我使用Chrome中的控制台搜索XPath,则XPath会导航到有问题的元素。

I also tried: 我也尝试过:

username = driver.find_element_by_name('username')

which also did not work. 这也没有用。

I am pretty new to Selenium, and I have no experience at all with HTML, so I don't know if there can be complications in the HTML that that must be taken into account when looking for elements through Selenium. 我对Selenium还是很陌生,并且对HTML完全没有经验,所以我不知道在通过Selenium查找元素时是否必须考虑到HTML中的复杂性。 Any help at all is appreciated. 任何帮助都表示赞赏。 This is also my first time posting here, so I hope I did not violate any rules. 这也是我第一次在这里发布,所以希望我没有违反任何规则。

Looks like the input box is taking a few seconds to load. 看起来输入框需要花费几秒钟来加载。 Try putting a delay before your request: 尝试在您的请求之前延迟一下:

driver.implicitly_wait(3)

If that doesn't work, increase the number in parentheses a second at a time. 如果这样不起作用,请一次增加括号中的数字。 If that solves the problem, you could instead do an explicit wait. 如果这样可以解决问题,则可以改为显式等待。

Try the following code: 尝试以下代码:

from selenium import webdriver
from selenium.webdriver.support import ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By


options = Options()
options.add_argument("unlimited-storage")
driver = webdriver.Chrome(chrome_options=options)

driver.get("http://www.runescape.com/companion/comapp.ws")

iframe = ui.WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.TAG_NAME, "iframe")))

# Switch to the frame.
driver.switch_to.frame(iframe)

username = ui.WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "username")))

print(username)

# Switch to the main content.
driver.switch_to.default_content()

driver.quit()

Hope it helps you! 希望对您有帮助!

As per the HTML you have shared the WebElement is a Angular element so you have to induce WebDriverWait for the element as follows : 按照您共享的HTMLWebElementAngular元素,因此您必须为该元素引入WebDriverWait ,如下所示:

  • CSS_SELECTOR : CSS_SELECTOR

     username = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.ng-pristine.ng-invalid.ng-invalid-required#username"))) 
  • XPATH : XPATH

     username = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='ng-pristine ng-invalid ng-invalid-required' and @id='username']"))) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Selenium 和 Salesforce - 似乎无法访问某些表单元素 - Python Selenium with Salesforce - Cannot Seem to Access Certain Form Elements 无法使用 selenium 和 Python 连接到网页 - Cannot connect to webpage using selenium and Python 使用 Python Selenium 访问网页上的表格对象 - Access to table objects on Webpage Using Python Selenium Selenium Python无法获取元素 - Selenium Python cannot get elements 使用 Selenium 和 Python 访问网页时,Selenium 无法单击元素 - Selenium unable to click on elements while accessing a webpage using Selenium and Python Python Selenium 访问内部元素 - Python Selenium access inner elements Selenium 和 Python——如何在延迟加载网页中等待元素被附加到父级? - Selenium and Python -- How to Wait for Elements to be Appended to Parent in a lazy loading webpage? Python 2.7 Selenium-如何获取网页上的所有元素? - Python 2.7 Selenium - How to get all elements on a webpage? Python Selenium - 获取属于网页特定部分的元素的 TimeoutException - Python Selenium - Getting TimeoutException for elements belonging to a particular section of a webpage 如何让 Python selenium chromedriver 加载网页中的所有元素? - How to make Python selenium chromedriver load all the elements in a webpage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM