英文:
Selenium find element and click
问题
我有这个HTML标记,我的目标是找到th
元素的类,然后使用Selenium点击它。
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "button.button-3.j-save.float_left.j-retailer-start-order-submit"))).click()
你的第一个尝试接近正确,但你不需要在By.CLASS_NAME
中使用a[class=...]
,只需提供类名即可。
英文:
I've this html markup , my target is find th element class an then click on it using selenium
<div class="float_left buttons">
<a href="#" class="button button-3 j-save float_left j-retailer-start-order-submit">Export</a>
</div>
I've tried :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "a[class='button button-3 j-save float_left j-retailer-start-order-submit']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[class='.button.button-3.j-save.float_left.j-retailer-start-order-submit']"))).click()
what is wrong?, tnx for your help
答案1
得分: 0
请尝试以下两行代码中的任一行:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='button button-3 j-save float_left j-retailer-start-order-submit']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Export']"))).click()
代码中的问题:
在下面的代码中,使用 By.CLASS_NAME
定位策略时,类名之间不应该有空格:
By.CLASS_NAME, "a[class='button button-3 j-save float_left j-retailer-start-order-submit']"
在下面的代码中,CSS选择器不应该在方括号内的每个类名前加点(.
)。而且,class属性应该只包含不带任何点的以空格分隔的类名:
By.CSS_SELECTOR, "a[class='.button.button-3.j-save.float_left.j-retailer-start-order-submit']"
英文:
Try either of the below two lines of code instead:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='button button-3 j-save float_left j-retailer-start-order-submit']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Export']"))).click()
Issues in your code:
In the below code, while using By.CLASS_NAME
locator strategy, there should not be space between class names
By.CLASS_NAME, "a[class='button button-3 j-save float_left j-retailer-start-order-submit']"
In the below code, The CSS selector should not contain dots (.
) before each class name within the square brackets. Also, the class attribute should only contain the space-separated class names without any dots.
By.CSS_SELECTOR, "a[class='.button.button-3.j-save.float_left.j-retailer-start-order-submit']"
答案2
得分: 0
这将点击该链接:
element = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, "a.j-save")
))
或者如果有多个元素具有j-save
类,可以使用以下代码:
element = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, "a.j-retailer-start-order-submit")
))
您还可以在a
标签元素上使用By.LINK_TEXT
,假设页面上只有一个名为"Export"的按钮。
英文:
This will click on that link:
element = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, "a.j-save")
))
Or if more than one element has the j-save
class, use this:
element = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, "a.j-retailer-start-order-submit")
))
You can also use By.LINK_TEXT
with "Export"
(for a
tag elements only), assuming that's the only Export
button on the page.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论