英文:
Using Python with Selenium to navigate Python - cannot find link?
问题
I am an accountant and very novice coder. I am trying to use Selenium with Python and Firefox browser to click through a series of webpages. I am just trying to select and click a hyperlink on the page, but I can't seem to locate it with Selenium. Please can someone show me the simplest way to select the hyperlink I want?
Imports:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
I then have code which successfully navigates to a webpage, inputs my username and password and submits the form. Then when I want to click a link on the next page:
time.sleep(5) # to make sure webpage loads before trying to find link
Link1 = browser.find_element(By.partialLinkText("/maf/app/auth")) # or any easier selector method?
Link1.click()
The error message is "AttributeError: type object 'By' has no attribute 'partialLinkText'."
When I inspect the link element it looks like this - not sure what the easiest element is to locate and subsequently click it?
<a target="_blank" title="Go to Orbitax" href="/maf/app/authentication/sso/ping/redirect?target=orbitax_chre">
Go to Orbitax
<img src="/cpw/images/rightBlueArrow.png" alt=""
width="5" height="8"></a>
Thanks in advance!
英文:
I am an accountant and very novice coder. I am trying to use Selenium with Python and Firefox browser to click through a series of webpages. I am just trying to select and click a hyperlink on the page, but I can't seem to locate it with Selenium. Please can someone show me the simplest way to select the hyperlink I want?
Imports:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
I then have code which successfully nagivates to a webpage, inputs my username and password and submits the form. Then when I want to click a link on the next page:
time.sleep(5) # to make sure webpage loads before trying to find link
Link1 = browser.find_element(By.partialLinkText ("/maf/app/auth")); # or any easier selector method?
Link1.click()
The error message is "AttributeError: type object 'By' has no attribute 'partialLinkText'"
When I inspect the link element it looks like this - not sure what the easiest element is to locate and subsequently click it?
<a target="_blank" title="Go to Orbitax" href="/maf/app/authentication/sso/ping/redirect?target=orbitax_chre">
Go to Orbitax
<img src="/cpw/images/rightBlueArrow.png" alt=">"
width="5" height="8"></a>
Thanks in advance!
答案1
得分: 1
# 请注意以下部分已被翻译,代码部分未变更:
time.sleep(5) # 确保网页加载完成后再尝试查找链接
Link1 = browser.find_element(By.PARTIAL_LINK_TEXT, "/maf/app/auth") # 或者是否有更简单的选择器方法?
Link1.click()
英文:
Your code snippet here
time.sleep(5) # to make sure webpage loads before trying to find link
Link1 = browser.find_element(By.partialLinkText ("/maf/app/auth")); # or any easier selector method?
Link1.click()
There is no By.partialLinkText
in the Selenium Python bindings. It should be
time.sleep(5) # to make sure webpage loads before trying to find link
Link1 = browser.find_element(By.PARTIAL_LINK_TEXT, "/maf/app/auth"); # or any easier selector method?
Link1.click()
答案2
得分: 0
要翻译的内容:
要么你正在使用旧版本的selenium
,要么import
没有正常工作。
尝试将selenium更新到最新版本。
或者
如果你正在使用旧版本的Chrome,可以使用以下代码。
import time
from selenium.webdriver.common.by import By
time.sleep(5)
link1 = browser.find_element(By.LINK_TEXT, "转到Orbitax")
link1.click()
英文:
Either you are using old version of selenium
or import
is not work correctly.
Try to update the selenium to the latest version.
OR
use the below code if you are using an old version of chrome.
time.sleep(5)
link1 = browser.find_element(By.LINK_TEXT, "Go to Orbitax")
link1.click()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论