[英]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 : 按照您共享的
HTML
, WebElement
是Angular
元素,因此您必须为该元素引入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.